import java.util.Scanner;
class Product{
//Class attributes
private String name;
private double price;
private int score;
public Product() {
name = "";
score = 0;
price = 1;
}
public void printData(){
System.out.println("Name: " + name);
System.out.println("Price: " + price);
System.out.println("Score: " + score);
}
public void read() {
Scanner in = new Scanner(System.in);
System.out.println("Product name: ");
String name = in.nextLine();
System.out.println("Price: ");
double price = in.nextDouble();
System.out.println("Score: ");
int core = in.nextInt();
}
public boolean is_better_than(Product other) {
if (score/price > other.score/other.price)
return true;
return false;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Product best = new Product();
boolean more = true;
while(more) {
Product current = new Product();
current.read(); //
if (current.is_better_than(best)) {
best = current;
}
System.out.println("More Products? 1:YES, 2:NO");
int answer = in.nextInt();
if (answer != 1)
more = false;
in.nextLine();
}
best.printData();
}
}
字符集
当我输入一个名字,一个分数和一个价格,它显示什么价格的变量被赋予在begining的programm,而不是新的
3条答案
按热度按时间a1o7rhls1#
在read方法中,你已经为name price和core创建了局部变量。你阅读这些局部变量,而不是你的类的成员变量(字段)。
而不是
字符集
你应该做
型
hts6caw32#
您已经声明了新的 local 变量来存储输入数据,而不是将其赋值给示例变量。
字符集
vc6uscn93#
你不必在 read 方法中重新声明这些值。
字符集
输出
型