0

我在 Java 中存储多个输入值时遇到问题。首先,我使用 ArrayList 来存储用户输入的输入数量。然后我想在收集所有输入后进行计算。

我的程序允许用户在 1 到 5 之间输入 5 个不同的值。1 等于 100,2 等于 200,3 等于 300,4 等于 400,5 等于 500

我创建一个数组来存储这些值

double numberArray[] = {100, 200, 300, 400, 500};

当用户输入 1 时,ArrayList 将存储第一个输入值。当用户输入 2 时,ArrayList 将存储第二个输入值,依此类推。当用户点击“n”时,它将退出并进行添加。这意味着它将 100 和 200 相加,输出将等于 300。

但是,问题是当用户继续输入数字时,即使我输入 2 作为第二个输入,我的程序也只会将第一个输入的总和相加,即 100 + 100。

这是我的代码:

import java.util.*;

public class Total{

   static int total;
   public static void main(String[] args) {

    int numberArray[] = {100, 200, 300, 400, 500};

    List<String> list = new ArrayList<String>();
    Scanner input = new Scanner(System.in);

    do{
        System.out.println("Add item? Please enter \"y\" or \"n\"");
        if (input.next().startsWith("y")){
            System.out.println("Enter item number: ");
            list.add(input.next());
            if (list.contains("1")){
                int item1 = numberArray[0];
                total = total + item1;
            } else if(list.contains("2")){
                int item2 = numberArray[1];
                total = total + item2;
            } else if(list.contains("3")){
                int item3 = numberArray[2];
                    total = total + item3;
            } else if(list.contains("4")){
                    int item4 = numberArray[3];
                total = total + item4;
            } else {
            System.out.println("You have entered invalid item number!");
            break;
            }               
        }else{
            System.out.println("You have entered all the item(s).");
            break;
        }       
     } while(true);
             System.out.println(The total is: " + total);
     }
}
4

5 回答 5

2

考虑一下我运行程序时会发生什么。

第一次运行:我回答'y';并输入1list.contains("1")评估为true。一切都很好,并且total=100
第二遍:我回答'y';并输入2。但是,list.contains("1")仍然评估为true- 哎呀!再次添加 100,显然应该添加 200。事实上,每次下一次运行都会添加 100,因为list.contains("1")从现在开始将始终如此。

不要使用列表来存储用户输入,String而是使用变量。更好的是,考虑使用input.nextInt()并查找表中的值。

另外,您忘记了“5”的情况。

于 2012-10-09T05:40:39.553 回答
1

您正在从扫描仪读取两次

list.add(input.next());
System.out.println(input.next());

但丢弃第二个输入......

更新

好的,这是我的看法

public class TestScanner03 {

    private static int total;

    public static void main(String[] args) {

        int numberArray[] = {100, 200, 300, 400, 500};

        List<String> list = new ArrayList<String>();
        Scanner input = new Scanner(System.in);

        // Better to use a stateful flag then simply "break" the loop
        boolean stay = true;
        do {
            System.out.println("Add item? Please enter \"y\" or \"n\"");
            String next = input.next();
            // Catch case issues
            if (next.equalsIgnoreCase("y")) {
                System.out.println("Enter item number: ");
                next = input.next();
                try {
                    // Make sure that the user actually entered a numeric value
                    int value = Integer.parseInt(next);
                    switch (value) {

                        // Make sure the value is within range
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                            // Make sure we don't already have the value
                            if (!list.contains(next)) {
                                // Extract the "addition" from the number array...
                                total += numberArray[value - 1];
                                list.add(next);
                                System.out.println("Your total is now " + total);
                            } else {
                                System.out.println(next + " is already used");
                            }
                            break;

                        default:
                            break;

                    }
                } catch (NumberFormatException numberFormatException) {
                    System.out.println(next + " is not a valid number");
                }

            } else {
                System.out.println("You have entered all the item(s).");
                stay = false;
            }
        } while (stay);
        System.out.println("Your total is " + total);
    }

}
于 2012-10-09T05:26:21.350 回答
1

应修改 IF 条件以获得预期的输出,如下所示

if (list.get(list.size() -1).contains("1"))

查看修复了所有错误的完整代码:

    import java.util.*; 

public class Total{ 

   static int total; 
   public static void main(String[] args) { 

    int numberArray[] = {100, 200, 300, 400, 500}; 

    List<String> list = new ArrayList<String>(); 
    Scanner input = new Scanner(System.in); 

    do{ 
        System.out.println("Add item? Please enter \"y\" or \"n\""); 
        if (input.next().startsWith("y")){ 
            System.out.println("Enter item number: "); 
            String temp = input.next(); //store the user inputed element
            list.add(temp); //add that element in list
            //System.out.println(input.next()); 

            //Now use the temporary variable in if conditions
            if (temp.contains("1")){ 
                int item1 = numberArray[0]; 
                total = total + item1; 
            } else if(temp.contains("2")){ 
                int item2 = numberArray[1]; 
                total = total + item2; 
            } else if(temp.contains("3")){ 
                int item3 = numberArray[2]; 
                    total = total + item3; 
            } else if(temp.contains("4")){ 
                    int item4 = numberArray[3]; 
                total = total + item4; 
            } else { 
            System.out.println("You have entered invalid item number!"); 
            break; 
            }                
        }else{ 
            System.out.println("You have entered all the item(s)."); 
            break; 
        }        
     } while(true);

        System.out.println("Total is: " + total);
    } 
} 
于 2012-10-09T05:37:57.650 回答
0

你的总数永远不会增加,相反,它只是在第 n 个位置打印元素。

            if (list.contains("1")){ //You entered 2, this is false, total = 0
                int item1 = numberArray[0];
                total = total + item1; // this doesn't get executed
            } else if(list.contains("2")){ //true
                int item2 = numberArray[1];
                total = total + item2; // total = 200
            } else if(list.contains("3")){ //false
                int item3 = numberArray[2];
                    total = total + item3; // still total = 200, as this is not executed
            } else if(list.contains("4")){ // false
                    int item4 = numberArray[3];
                total = total + item4; // not executed, still total = 200
            } 

因此,更好的解决方案是使用 switch 语句,或者将输入转换为 int。

int input = Integer.parseInt(list.get(0));
if(input < 5)
{
  for(int i = 0; i< input; i++)
  total += numberArray[i];
}
于 2012-10-09T05:29:45.373 回答
0

只需这样尝试....

将值分配给 String 变量,然后重试...

String getUserInput = new Scanner(System.in).next();

编码:

do{
        System.out.println("Add item? Please enter \"y\" or \"n\"");

        String getUserInput = new Scanner(System.in).next();

        if (getUserInput.startsWith("y")){
            System.out.println("Enter item number: ");
            list.add(getUserInput);
            System.out.println(getUserInput);
            if (list.contains("1")){
                int item1 = numberArray[0];
                total = total + item1;
            } else if(list.contains("2")){
                int item2 = numberArray[1];
                total = total + item2;
            } else if(list.contains("3")){
                int item3 = numberArray[2];
                    total = total + item3;
            } else if(list.contains("4")){
                    int item4 = numberArray[3];
                total = total + item4;
            } else {
            System.out.println("You have entered invalid item number!");
            break;
            }               
        }else{
            System.out.println("You have entered all the item(s).");
            break;
        }       
     } while(true); 
     }
于 2012-10-09T05:32:49.520 回答