Java赋值不知道是什么错误

时间:2013-11-13 21:02:38

标签: java

问题是:

水果店每天销售几种水果。编写一个程序,从用户读取几行输入。每行包括一个水果的名称,每千克的价格(作为整数),销售的公斤数(作为整数)。

该计划应计算并打印所有销售的水果和获得最大利润的水果的收入。

提示: - 您可以假设用户将插入有效数据 - 用户可以通过输入单词“stop”作为水果的名称来停止程序。

示例输入和输出:

在每一行中,插入水果的名称,每千克的价格,销售的公斤数。要暂停程序,请将“停止”作为水果名称插入


香蕉2 11 芒果3 8 桃4 5

停止


所售水果的赚取金额:66 获得最大利润的水果:芒果


我现在写的是什么:

public static void main(String[] args) { 
// TODO code application logic here 
Scanner input = new Scanner (System.in); 
String fruitname= " "; 
String maxfruit = " "; 
int price = 0,number=0; 
int sum=0; 
int max=0; 


System.out.print("Fruit name, " + "price in killogram, number of killogram sold: "); 

while (!fruitname.equals("stop")) 
{ 
fruitname = input.next(); 
price = input.nextInt(); 
number = input.nextInt(); 
} 
if (fruitname.equals("stop")) 
{ 
sum = sum+(price*number); 

} 
if (max<(price*number)) 
{ 
max = price*number; 
maxfruit = fruitname; 
} 


System.out.println("the earned money of all fruits is " + sum); 
System.out.println("fruit that achieved the largest profit is "+ maxfruit); 
} 
} 

该程序没有阅读我提交给它的内容,不知道为什么而不给我总和和最大的成果..我写的是什么问题?

5 个答案:

答案 0 :(得分:1)

正如您可以看到您的读取发生在while循环中:

while (!fruitname.equals("stop")) 
{ 
    fruitname = input.next(); 
    price = input.nextInt(); 
    number = input.nextInt(); 
} 

每次循环时 - 它都会覆盖值。最后,当您阅读停止并退出循环时 - 您的fruitname 停止。所以你需要修改你想要在输入中读取的逻辑

答案 1 :(得分:1)

工作变体:

public class FruitTest {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);


    System.out.print("Fruit name, " + "price in killogram, number of killogram sold: ");

    String text = input.nextLine();

    String[] words = text.split(" ");

    List<Fruit> fruits = parseInput(words);

    int sum = getSum(fruits);

    String popular = getPopularFruitName(fruits);

    System.out.println("Got fruits: " + fruits.toString());
    System.out.println("the earned money of all fruits is " + sum);
    System.out.println("fruit that achieved the largest profit is " + popular);
}

private static String getPopularFruitName(List<Fruit> fruits) {
    int max = 0;
    String name = null;

    for (Fruit fruit : fruits) {
        int checkVal = fruit.getPrice() * fruit.getAmount();
        if(checkVal > max) {
            max  = checkVal;
            name = fruit.getName();
        }
    }

    return name;
}

private static int getSum(List<Fruit> fruits) {
    int result = 0;
    for (Fruit fruit : fruits) {
        result += fruit.getPrice() * fruit.getAmount();
    }
    return result;
}

private static List<Fruit> parseInput(String[] words) {
    List<Fruit> result = new ArrayList<Fruit>();
    int element = 1;
    final int name = 1;
    final int price = 2;
    final int amount = 3;

    Fruit fruit = null;
    for (String word : words) {
        if (word.equals("stop") || word.isEmpty()) {
            break;
        }
        if(element > amount)
            element = name;

        switch (element) {
            case name:
                fruit = new Fruit(word);
                result.add(fruit);
                break;
            case price:
                if (fruit != null) {
                    fruit.setPrice(Integer.valueOf(word));
                }
                break;
            case amount:
                if(fruit != null) {
                    fruit.setAmount(Integer.valueOf(word));
                }
                break;
        }
        element++;
    }

    return result;
}

static class Fruit {
    String name;
    int price  = 0;
    int amount = 0;


    Fruit(String name) {
        this.name = name;
    }

    String getName() {
        return name;
    }

    int getPrice() {
        return price;
    }

    void setPrice(int price) {
        this.price = price;
    }

    int getAmount() {
        return amount;
    }

    void setAmount(int amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        return name + ". $" + price +
               ", amount=" + amount;
    }
}
}

对代码的评论 - 这是解析所有输入字符串并将其解析为存储所有数据的对象(名称,价格和金额)的正确方法。将所有已解析的对象存储到数组或列表中,然后在循环解析的水果数组时计算最大和常用水果

答案 2 :(得分:0)

我发现了一些错误。最重要的是在条件下。看看这个。

public static void main(String[] args) { 
    // TODO code application logic here 
    Scanner input = new Scanner (System.in); 
    String fruitname = null;
    String maxfruit = null;
    int fruitSum = 0;
    int totalSum = 0; 
    int max = 0; 

    System.out.print("Fruit name, " + "price in killogram, number of killogram sold: "); 


    while(!(fruitname = input.next()).equals("stop")){
        fruitSum = input.nextInt() * input.nextInt();
        totalSum += fruitSum;
        if(fruitSum > max){
            maxfruit = fruitname;
            max = fruitSum;
        }

    }

    System.out.println("the earned money of all fruits is " + totalSum); 
    System.out.println("fruit that achieved the largest profit is "+ maxfruit); 
} 
} 

答案 3 :(得分:0)

哦,它正在读它。

问题在于它没有做你想做的事。

我能看到的代码问题是:

  • 您没有在任何地方存储水果数量或价格,您需要存储值 在数组或其他东西(maxFruit,MaxValue)中稍后进行比较。
  • 当您正在读取水果值并输入“停止”字符串时,代码中的下一步是等待价格,以便即使您输入“停止”也不会退出循环,您需要重构您的扫描仪循环。

如果它是一个初学者类,它可能没问题,但是你编写的代码不是面向对象的,不会在主编写逻辑。

您可能想要学习调试它是一个非常有用的工具,当您学习编码时,如果您在调试模式下运行此程序,您可以看到值正在获取输入和正在发生的一切,Netbeans和Eclipse有非常好的调试器,花半小时学习调试的基础是值得的。当我开始时,这对我帮助很大。

答案 4 :(得分:0)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FruitSells {
    public static void main(String... args) {
        BufferedReader bufer = new BufferedReader(new InputStreamReader(System.in));
        try {
            String str;
            String[] inarr;
            int sumMoney = 0;
            do {
                str = (String) bufer.readLine();
                inarr = str.split(" ");
                for(int i = 1; i < inarr.length; i += 3) {
                    sumMoney += Integer.parseInt(inarr[i]) * Integer.parseInt(inarr[i + 1]);
                }
                System.out.println(sumMoney);
                sumMoney = 0;
            } while (!str.equals("stop"));


        } catch(IOException ex) {
            System.out.println("Problems with bufer.readLine()");
        }
    }
}

像这样你可以现代化它。对于我来说,我不会说话))并且当然写得正确))