2017-11-30 1 views
0

最初の静的ブロックで入力値を取得できません。共通ブロック間に入力値を保存

これは私のコードです:

public static void main(String args[])throws IOException{ 

    int i,people,timeai,tablenoai; 
    double iprice,timebi,tablenobi; 
    char decider1; 
    String name; 

    InputStreamReader read=new InputStreamReader(System.in); 
    BufferedReader in=new BufferedReader(read); 

    System.out.println("Welcome To Joel's Restaurant"); 
    System.out.println("How Many People Do You Have?"); 
    people=Integer.parseInt(in.readLine()); 
    System.out.println("And May I Know Your Good Name?"); 
    name=in.readLine(); 
} 
    public static void sleep(The_Restaurant millis) throws InterruptedException{ 
    double timebi; 
    int timeai; 
    timebi=Math.random()*1000; 
    timeai=(int)timebi; 
    Thread.sleep(timeai); 
} 
public static void main(The_Restaurant args[])throws IOException{ 
    int tablenoai; 
    double tablenobi; 
    tablenobi=Math.random()*10; 
    tablenoai=(int)tablenobi; 
    System.out.println("Mr."+name+"Table For"+people+"Your Table No Is"+tablenoai); 
} 

はそれを行うための方法はありますか?

+1

あなたはそれぞれ、すべての単語を大文字にしてはいけません。 「あなたが他の誤りに気付いた場合」のような文章は良い質問ではありません。適切な字下げを行うようにコードを書式化してください。 –

+0

「あなたの良い名前を知っていますか?」中世のパロディーレストランのアプリですか?多分現代英語のチュートリアルをチェックしてください。彼らがこれまでにしたことがないなら、誰も約300年ほど話したことはありません。 – ADyson

答えて

0

尋ねられていることは明確ではありません。明らかに質問を編集して改善することができます。あなたは新しいstackoverflowので、おそらく既存の質問を見て、それをより鮮明で読みやすいように整形してみてください。 ある静的メソッドから値を読み込み、別の静的メソッドで読み込む方法が不思議ならば、おそらくグローバル変数を使用できます。 静的ブロックを使用しようと思っている場合(あなたのユースケースには適合しません)、静的ブロックの動作は順番に実行されます。 read this

あなたのコードにいくつかの問題 -

  • あなたは、いくつかのIO APIがユーザーの入力を待ってロジックを待つ必要はありません。
  • mainメソッドは常にjvmのエントリポイントです。複数のmainを作成しても、すべてがjvmから呼び出せるわけではありません。
  • 不必要な変数がたくさん作成されました。ここで

あなたも参照がかかる場合があり、コードの小片である -

private static int[] TABLES = new int[] {1, 2, 3, 4, 5, 6, 7}; 
public static void main(String[] args) { 
    System.out.println("Welcome to Joel's Restaurant."); 
    while (true) { 
     System.out.println("please enter quit to exit. For booking write you name."); 
     try (Scanner scanner = new Scanner(System.in)) { 
      final String name = scanner.nextLine(); 
      if ("quit".equals(name.toLowerCase())) { 
       break; 
      } 
      System.out.print("Number of people accompanying you: "); 
      final int count = scanner.nextInt(); 
      final int randomTable = Double.valueOf(Math.random()).intValue() % 7; 
      System.out.println("Mr. " + name + ", table no." + TABLES[randomTable] + " is assigned to you with count: " + count + "."); 
      System.out.println(); 
     } 
    } 

} 
関連する問題