这道题怎么写?(Java)

2024-11-30 09:31:41
推荐回答(4个)
回答(1):

import java.util.Random;

public class QuessMachine {
public int init(){
Random random=new Random();
int num= 1+random.nextInt(10);
System.out.println("这个为商品"+num);
return num*100;
}
public boolean guess(int price,int guessNum){
if (guessNum> price) {
System.out.println("再小点");
return false;
}else if (guessNum< price) {
System.out.println("再大点");
return false;
}else{
System.out.println("猜对了");
return true;
}
}

}

下面是测试类
import java.util.Scanner;

public class Test {
public static void main(String[] args) {
QuessMachine quessMachine=new QuessMachine();
int price=quessMachine.init();
int count=3;
Scanner input=new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请猜第"+(i+1)+"次");
boolean flag= quessMachine.guess(price,input.nextInt());
if (flag) {
System.out.println("获得奖品");
break;
}
count--;
}
if (count==0) {
System.out.println("抱歉,没猜对");
}
}
}

要注释的请多加点分

补充:
其实我这样写严格来说是不是太合理的。应该是return 出 一个对象 的。
但是鉴于你刚学并且基础不是很好,也不宜写得太深。
给你改好了。你看下

回答(2):

Java参考源代码:

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;


public class Test10 extends JFrame implements ActionListener {

protected JList lstLeft = null;

protected JList lstRight = null;

protected JButton btnAdd = null;

protected String[] arr = {"新闻", "娱乐", "体育", "教育"};


public Test10() {

super("列表框");


initComponent();


this.setSize(400, 300);

this.setVisible(true);

this.setLayout(new FlowLayout());

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}


public void initComponent() {

lstLeft = new JList(arr);

lstRight = new JList();

btnAdd = new JButton(">");


this.add(lstLeft);

this.add(btnAdd);

this.add(lstRight);


lstLeft.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

btnAdd.addActionListener(this);

}


public static void main(String[] args) {

new Test10();

}


@Override

public void actionPerformed(ActionEvent e) {

Object[] items = lstLeft.getSelectedValues();

DefaultListModel model = new DefaultListModel();

lstRight.setModel(model);

model.removeAllElements();


for(Object value : items) {

model.addElement(value);

}

}

}




运行测试:


请点击输入图片描述

回答(3):

展开看(点右下角):
import java.util.Scanner;
public class QuessMachine {
int jiaGe=0;
static String pd;
static int pdPrise;
public void initial(){
int x=(int)(Math.random()*5);
switch(x){
case 0:
pd="洗衣机";
pdPrise=599;
break;
case 1:
pd="电视机";
pdPrise=3999;
break;
case 2:
pd="电冰箱";
pdPrise=4999;
break;
case 3:
pd="微波炉";
pdPrise=899;
break;
case 4:
pd="电饭煲";
pdPrise=299;
break;
}

}
public void guess(){
int i;
Scanner sn=new Scanner(System.in);
System.out.println("请猜测" +"“"+pd+"”"+"的价格:");
for(i=0; i<4; i++)
{
int prise=sn.nextInt();
if(prise==pdPrise)
{
System.out.println("你猜对了,恭喜你获得该商品。");
break;
}
else if(priseSystem.out.println("再大些,再猜一次吧:");
else
System.out.println("再小些,再猜一次吧:");
}
if(i==4)
{
System.out.println("4次内没有猜对,下次努力吧!");
}
}
}
测试类:

public class TestQuessMachine {
public static void main(String[] args) {
// TODO Auto-generated method stub
QuessMachine a=new QuessMachine();

a.initial();
a.guess();
}

}

回答(4):

现编写一个电视的基类
然后在quessmachine中 的initial方法中 进行 初始赋值
quess方法 调用 电视的基类的属性 进行比较
返回是不成功