题目一:
参考代码
importjava.util.Scanner;
publicclassJiaFaDemo{
publicstaticvoidmain(String[]args){
intx=(int)(Math.random()*100);
inty=(int)(Math.random()*100);
System.out.println(x+"+"+y+"="+(x+y));//自动答题58+4=62
//System.out.print(x+"+"+y+"=");//人工输入答题需要导入包importjava.util.Scanner;
//Scannerinput=newScanner(System.in);
//intz=input.nextInt();
//if(z==(x+y)){
//System.out.println("回答正确");
//}else{
//System.out.println("回答错误");
//}
}
}
输出
58+4=62
题目二
publicclassJiShuDemo{
publicstaticvoidmain(String[]args){
intsum=0;
for(inti=1;i<=50;i++){
if(i%2!=0){//不能被2整除的数是奇数
sum+=i;
}
}
System.out.println("1~50间奇数和="+sum);
}
}
输出
1~50间奇数和=625
❷ 这道java编程题怎么做
import java.util.*;
class Test1{
public LinkedList res=new LinkedList();
public Scanner item=new Scanner(System.in);
}
class Test2 extends Test1{
private void calc(){
System.out.println("输入4个字符");
for(int x=0;x<4;x++){
char tem=item.next().charAt(0);
if(Character.isUpperCase(tem)){
tem=Character.toLowerCase(tem);
res.add(tem);
}else if(Character.isLowerCase(tem)){
tem=Character.toUpperCase(tem);
res.add(tem);
}else if(Character.isDigit(tem)){
res.add(tem);
}else{
String str="other";
res.add(str);
}
}
for(Object y:res){
System.out.println(y);
}
}
public static void main(String[] args){
new Test2().calc();
}
}
❸ JAVA编程基础题目,怎么写,求大神详细指导,谢谢!
//代码如下
import java.until.Scanner;
Class Test(){
float dc;//表示折扣
public static void main(String []arg0)
{
Scanner sca=new Scanner(System.in);
System.out.println(“请输入是否为会员:是(y)/否(其他字符)”);
String a=sca.next();
System.out.println(“请输入购物金额:”);
float b=sca.nextFloat();
//嵌套if代码段
if(a.equal("y"))
{
if(b>=200) dc=0.75
else dc=8
}
else{
if(b>=100) dc=0.9
else dc=1
}
float count=b*dc;//实际支付金额
Sytem.out.println("实际支付:"+count);//最后打印
}
}
❹ java编程题,期末考试的题,跪求,求大佬编写一下
publicclassRunTest{
privatestaticint[]arr={2,5,7,1,3,4,6,9,8};
publicstaticvoidmain(String[]args){
System.out.println("原始:"+Arrays.toString(arr));
newThread(){
publicvoidrun(){
StringBuildersd=newStringBuilder();
for(inta:arr){
sd.append(a+"");
}
System.out.println("反转:["+sd.reverse()+"]");
}
}.start();
newThread(){
publicvoidrun(){
for(inti=0;i<arr.length-1;i++){
for(intj=0;j<arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
inttemp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println("冒泡:"+Arrays.toString(arr));
}
}.start();
}
}
❺ JAVA编程题!
//把你的那个表作成test3.txt放到D盘根,跑程序就好了
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class MyTest3 {
List stuInfoList = new ArrayList();
public MyTest3(){
printResult();
}
public void readFile() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\test3.txt")));
String line = "";
int i=0;
while ((line = br.readLine()) != null) {
if(i++>0){ //略过头上的汉字行
StudentInfo student = new StudentInfo(line.split(" "));
stuInfoList.add(student);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
class StudentInfo implements Comparable{
public int stuId;
public double pings;
public double qizhong;
public double qimo;
public double bishi;
public double zhongFeng;
public StudentInfo(){};
public StudentInfo(String[] info){
this.stuId = Integer.parseInt(info[0]);
this.pings = Integer.parseInt(info[1]);
this.qizhong = Integer.parseInt(info[2]);
this.qimo = Integer.parseInt(info[3]);
this.bishi = Integer.parseInt(info[4]);
this.zhongFeng = pings*0.1+qizhong*0.25+qimo*0.15+bishi*0.5;
}
public String getPingJunFeng(int size){
return pings/size +" "+qizhong/size+" "+qimo/size+" "+bishi/size+" "+zhongFeng/size;
}
public String toString(){
return stuId + " " +pings + " "+qizhong+" "+qimo+" "+bishi+" "+zhongFeng;
}
public int compareTo(Object arg0) {
StudentInfo info = (StudentInfo)arg0;
return (int)(info.zhongFeng-this.zhongFeng);
}
}
public void printResult(){
readFile();
System.out.println("学号 平时 期中 期末 笔试 总评分");
for(Iterator it=stuInfoList.iterator();it.hasNext();){
System.out.println(it.next());
}
System.out.println("-----------80分以上---------------\r\n学号 总评分");
for(Iterator it=stuInfoList.iterator();it.hasNext();){
StudentInfo info = (StudentInfo)it.next();
if(info.zhongFeng>=80)
System.out.println(info.stuId + " "+info.zhongFeng);
}
System.out.println("-----------没有及格---------------\r\n学号 总评分");
for(Iterator it=stuInfoList.iterator();it.hasNext();){
StudentInfo info = (StudentInfo)it.next();
if(info.zhongFeng<60)
System.out.println(info.stuId + " "+info.zhongFeng);
}
Collections.sort(stuInfoList);
System.out.println("-----------排序之后---------------\r\n学号 平时 期中 期末 笔试 总评分");
for(Iterator it=stuInfoList.iterator();it.hasNext();){
System.out.println(it.next());
}
StudentInfo pinjunfeng = new StudentInfo();
for(Iterator it=stuInfoList.iterator();it.hasNext();){
StudentInfo info = (StudentInfo)it.next();
pinjunfeng.bishi+=info.bishi;
pinjunfeng.pings+=info.pings;
pinjunfeng.qimo+=info.qimo;
pinjunfeng.qizhong+=info.qizhong;
pinjunfeng.zhongFeng+=info.zhongFeng;
}
System.out.println("-----------平均分---------------\r\n平时 期中 期末 笔试 总评分");
System.out.println(pinjunfeng.getPingJunFeng(stuInfoList.size()));
}
public static void main(String[] args) throws Exception {
new MyTest3();
}
}
❻ 大一编程入门java语言,这道题代码怎么打呢
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PainterPanel extends JPanel implements MouseListener{
int shape=-1; //图案类型
Point[] point=new Point[2]; //记录鼠标拖动的起始点和终点
public PainterPanel(){
super(); //调用父类构造函数
this.setBackground(Color.white); //设置背景颜色
point[0]=new Point(-1,-1); //初始化变量
point[1]=new Point(-1,-1);
addMouseListener(this); //增加鼠标事件
}
public void mouseReleased(MouseEvent e){ //鼠标释放事件
point[1]=new Point(e.getX(),e.getY()); //设置终点位置
repaint(); //重绘屏幕
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){ //鼠标按下时事件
point[0]=new Point(e.getX(),e.getY()); //设置起始位置
}
public void paint(Graphics g){
super.paint(g);
switch (shape){ //根据shape绘制图形
case 0:
g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //绘线
break;
case 1:
int width=point[1].x-point[0].x;
int height=point[1].y-point[0].y;
g.drawOval(point[0].x,point[0].y,width,height); //绘椭圆
break;
case 2:
width=point[1].x-point[0].x;
height=point[1].y-point[0].y;
g.drawRect(point[0].x,point[0].y,width,height); //绘矩形
break;
}
}
public void drawShape(int shape){
this.shape=shape;
}
}
❼ java编程题目,明天考试用,急啊!!
第一题代码太多了,这里不方便写了
第二题还是比较简单,只需要针对字符串每个字符遍历(String.length),当然题目里面要求用while循环,否则直接用一个方法就可以了。当然还是需要统计转换的个数。这里给你写个主要的内容吧,其他自己写。
int i=0;
int count=0;
string str1;
while (i<str.length)
{
if (str.indexOf(i)>=97 && str.indexOf(i)<=123)
{
str1+=str.indexOf(i)-32;//这个地方不好,但是对于这个题目没关系
count++;
}
else
{
str1+=str.indexOf(i);
}
}
第三题也不难,会定义数组就一定会,但是这个里面也有问题,应为JAVA从键盘里读入数据,好像没那么简单,不过,这个题目里面,这部分不写也不会有太大问题。
public class Test
{
public static void main(string[] args)
{
int count= convert(args);//转换成数值
String[] num=new String(count);
//遍历
for (int i=0;i<num.count;i++)
{
//这里面完成累加就可以了。
}
}
}
我只能给你这么多了,再给你答案就是害你了。努力吧
❽ java编程题目这个怎么弄啊。不会啊求解答
Suansu.java
public class Suansu{
private int a;
private int b;
public Suansu(){ //构造函数 初始化a=10,b=5;
this.a=10;
this.b=5;
}
public int AddAB(){ //和
return a+b;
}
public int SubAB(){ //差
return a-b;
}
}
TestSuansu.java
public class TestSuansu{
public static void main(String[] args){
Suansu ss = new Suansu();
System.out.println("a + b = "+ ss.AddAB());
System.out.println("a - b = "+ ss.SubAB());
}
}
❾ java面向对象程序设计期末考试编程题!急!!!
===============================第一题==============================
import java.applet.Applet;
import java.awt.Color;
import java.awt.Label;
public class test extends Applet {
private Label label;
@Override
public void init() {
label=new Label("欢迎来到java世界!");
label.setBackground(Color.BLUE);
setBackground(Color.PINK);
add(label);
}
}
===============================第二题==============================
因为没图,所以自己设计的界面..
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class test implements ActionListener {
private JFrame frame;
private JLabel label;
private JTextArea jt;
private JScrollPane jsp;
private JButton show;
public test() {
frame=new JFrame("Test");
label=new JLabel("显示内容");
jt=new JTextArea(10,20);
jsp=new JScrollPane(jt);
show = new JButton("显示给定内容");
JPanel panel=new JPanel();
panel.add(label);
panel.add(show);
frame.add(jsp,BorderLayout.NORTH);
frame.add(panel,BorderLayout.SOUTH);
show.addActionListener(this);
show();
}
public void show(){
frame.setLocation(200, 200);
frame.setSize(300, 260);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
label.setText(jt.getText());
}
public static void main(String[] args) {
new test().show();
}
}
❿ java的编程题,要把代码答在卷子上要怎么写呢
{
privateintscore;
publicvoidsetScore(intscore){
this.score=score;
}
publicintgetScore(){
returnthis,score;
}
//show方法
publicvoidshow(){
System.out.println(this.getName()+":"+score);
}
//还要实现接口的其他方法
}
下面是测试单元:
@Test
public void test(){
Student s = new Student();
s.setName("sss");
s.setScore(88);
s.show();
}