无法在Java中存储多个输入值

时间:2012-10-09 05:17:30

标签: java arrays arraylist

我遇到了在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。

然而,问题是当用户保持输入数字时,我的程序只会将第一个输入的总和加起来为100 + 100,即使我输入2作为第二个输入。

这是我的代码:

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);
     }
}

5 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分: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);
    }

}

答案 2 :(得分: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);
    } 
} 

答案 3 :(得分: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];
}

答案 4 :(得分: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); 
     }