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は2番目の入力値を格納します。ユーザーが「n」を押すと、終了して追加を行います。これは、100と200を足し合わせて、出力が300になることを意味します。

ただし、問題は、ユーザーが数値を入力し続けると、2番目の入力として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'ます; と入力し1ます。list.contains("1")に評価されtrueます。すべてが順調です、そしてtotal=100
2番目の実行:私は答え'y'ます; と入力し2ます。しかし、list.contains("1")それでも評価されtrueます-おっと!明らかに200を追加する必要がある場合は、100を再度追加します。list.contains("1")実際、今後は常に当てはまるため、次回の実行ごとに100が追加されます。

リストを使用してユーザー入力を格納するString代わりに、変数を使用してください。さらに良いことに、の使用を検討しinput.nextInt()、表で値を調べてください。

また、「5」の場合も忘れてしまいました。

于 2012-10-09T05:40:39.553 に答える
1

スキャナーから2回読んでいます

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

しかし、2番目の入力を破棄します...

アップデート

さて、これが私の見解です

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 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 に答える