2017-01-29 12 views
-3

私は初心者で、私のプログラムに何が間違っているか分かりません。私が行うためのプログラムをしたいと思い何スキャナのメソッドが機能せず、whileループが中断しない

は、ユーザがMAXの下にある整数(1 000 000 000)を入力できるようにすることですが、0の上に、私もそのようにtrycatchメソッドを使用して例外ブロックを入れていますユーザーは文字列を入力できません。

例外ブロックは機能しますが、ブロックしたいブロックはifブロック内にも印刷されます。私は、ユーザーが文字列を入力した後、それは... Please enter a number not a word Try again:を印刷したい

Number of marbles to divide: 
*three* 
Please enter a number not a word 
Try again: 
Please enter a number under 1 000 000 000 and above 0 
Try again: 

これはどのようなコンソールプリントです。つまり、ユーザが文字列を入力した場合、メソッドのwhileループを中断させたいということです。

解決できないと思われるもう一つの問題は、値がMAX(1 000 000 000)を超えるIntegerを入力すると、プログラムが続行されるということです。これはコンソールが印刷するものです。

Number of marbles to divide: 
1000000001 
Number of people: 

あなたがプログラムは、ユーザがMAX以上の整数を入力しているにもかかわらず、続けて見ることができるように(1 000 000 000)

これは私のコードです:

import java.util.*; 

public class MarblesApp 
{ 
    final static int MAX = 1000000000; 
    static int numberOfMarbles; 
    static int numberOfPeople, marblesPerPerson, marblesLeftOver; 
    static Scanner input = new Scanner(System.in); 
    public static void main(String[] args) 
    { 
     System.out.println("Welcome to the marble divvy-upper."); 
     System.out.println("This program will tell you how many marbles to give to each person.\n" 
    + "The maximum amount of marbles is 1 000 000 000. The maximum amount of people is the same.\n"); 

     System.out.println("Number of marbles to divide: "); 
      numberOfMarbles = GetMarbles(); 

     System.out.println("Number of people: "); 
      numberOfPeople = GetPeople(); 

     marblesPerPerson = (int)numberOfMarbles/numberOfPeople; 
     marblesLeftOver = (int)numberOfMarbles % numberOfPeople; 

     System.out.println("Give each child " + marblesPerPerson + " marbles."); 
     System.out.println("You will have " + marblesLeftOver + " marbles left over."); 
    } 
private static int GetPeople() 
{ 
    while (true) 
    { 
     try 
     { 
      return input.nextInt(); 
     } 
     catch(InputMismatchException f) 
     { 
      input.next(); 
      System.out.println("Please enter a number not a word\nTry again: "); 
     } 

     if(numberOfPeople > MAX || numberOfPeople == 0); 
     { 
      System.out.println("Please enter a number under 1 000 000 000 and above 0\nTry again: "); 
     } 
    } 
} 
public static int GetMarbles() 
{ 
    while (true) 
    { 
     try 
     { 
      return input.nextInt(); 
     } 
     catch (InputMismatchException e) 
     { 
      input.next(); 
      System.out.println("Please enter a number not a word\nTry again: "); 
     } 

     if(numberOfMarbles > MAX || numberOfMarbles == 0); 
     { 
      System.out.println("Please enter a number under 1 000 000 000 and above 0\nTry again: "); 
     } 

     } 
    } 
} 
+0

エッセイとしてではなく、ポイントに質問を書き込んでください。 –

+0

多分、コンソールはプログラムに "123"の代わりに "123 \ n"を返しますが、別の解析メソッドを試してみてください。 – Gala

+0

stdinから読み込んだものは、スキャナによって対応するデータ型/オブジェクトに解析されない限り、文字列としてのみ読み込まれます。だから、あなたがそれを解析しない限り、スキャナが何を読んでいるのかを知ることはできません。 –

答えて

2

いかに難しいか、私は知っていますもしあなたが全く新しいものであれば、Javaが可能になります。あなたの質問が完璧に書かれ、その要点に導かれることを期待する人ではないので、私はあなたの問題に対する解決策をまとめました。

私はあなたのコードを少し再編成して絞る必要がありました。

  • Javaでメソッド名は常にあなたがそれらを
  • を必要としない場合は、グローバル変数を避けるため、小文字で始まる_または$
  • :そして、私はクリーンなコードを参照するいくつかの発言を確認する必要があります
  • 名前の異なる重複したメソッドを避ける

このコードでは、あなたがJAVAを使い始めることを願っています。楽しむ!

import java.util.*; 

public class MarblesApp 
{ 
    private final static int MAX = 1000000000; 

    static Scanner input = new Scanner(System.in); 

    public static void main(String[] args) 
    { 
     int numberOfMarbles, numberOfPeople, marblesPerPerson, marblesLeftOver; 

     System.out.println("Welcome to the marble divvy-upper."); 
     System.out.println("This program will tell you how many marbles to give to each person.\n" 
    + "The maximum amount of marbles is 1 000 000 000. The maximum amount of people is the same.\n"); 

     System.out.println("Number of marbles to divide: "); 
      numberOfMarbles = getNumberFromConsole(); 

     System.out.println("Number of people: "); 
      numberOfPeople = getNumberFromConsole(); 

     marblesPerPerson = (int)numberOfMarbles/numberOfPeople; 
     marblesLeftOver = (int)numberOfMarbles % numberOfPeople; 

     System.out.println("Give each child " + marblesPerPerson + " marbles."); 
     System.out.println("You will have " + marblesLeftOver + " marbles left over."); 
    } 

    public static int getNumberFromConsole() 
    { 
     int number; 

     while (true) 
     { 
      try 
      { 
       // get the number from console 
       number = input.nextInt(); 

       // validate whether it's greater zero and lower MAX 
       if(validateNumber(number) == true) 
       { 
        // if true, return the number 
        return number; 
       } 
       else 
       { 
        // if not, input again 
        input.next(); 
       } 
      } 
      catch (InputMismatchException e) 
      { 
       System.out.println("Please enter a number not a word\nTry again: "); 
       input.next(); 
      } 
     } 
    } 

    private static boolean validateNumber(int number) { 

     if(number > MAX || number == 0) 
     { 
      System.out.println("Please enter a number under 1 000 000 000 and above 0\nTry again: "); 
      return false; 
     } 

     return true; 
    } 
} 
+0

ありがとうございました。私はあなたに感謝しています –

関連する問題