2016-08-30 5 views
1

このプログラムは動作しますが、実行されますが、私はそれが馬鹿な証拠であることを望みます!そして、私はいくつかの助けが必要です。 ユーザーが何も入力しなかった場合、次の行に移動したくありません!パラメータが満たされていないとwhileループを再起動します

何も書き込まずにEnterキーを押すと、次の行に移動し、変数navnが最後に完全に空になります。 数字を書いても同じことが起こります。答えがプロンプトを満たしていない場合は、ループを再実行しながら同じ方法を試してみてください。 はどうもありがとうございました:)

import java.util.Scanner; 

class Metoder { 

    public static void main(String[] args) { 
     String bosted; //Variable 
     String navn; //Variable 

     Scanner in = new Scanner(System.in); 

     System.out.println("Skriv inn navn: "); //What shows up when you first start the program 

     while (!in.hasNext("[A-Za-z]+")) { //Only allow letters A-Z 
      in.next(); 
      System.out.println("Tall horer ikke hjemme i navn, prøv igjen!"); //Prints, "numbers dont belong in names, try again" if what the user entered is a number 

     } 
     System.out.println("Takk!"); //Says thank you if the user has entered letters 

     navn = in.nextLine(); //Proceeds to next line 

     System.out.println("Skriv inn bosted: "); //Next line, where the user is supposed to enter where he/she lives 
     while (!in.hasNext("[A-Za-z]+")) { //Excactly the same loop as above 
      in.next(); 
      System.out.println("Tall hører ikke hjemme i stedsnavn, prøv igjen!"); 
     } 
     System.out.println("Takk!"); 

     bosted = in.nextLine(); 

     System.out.println("Hei, " + navn + "! Du er fra " + bosted + "."); //Prints out what the user has entered previously in a full sentence. 

    } 
} 
+1

あなたは数回のコードの同じ部分を使用する場合は、専用のメソッドの中にそれを置くことを検討してください。 – Berger

+0

はい、ありがとうございます! –

+0

whileループ内のすべてをputすると、変数に任意の値が含まれている場合のみwhileループが終了します。 –

答えて

1

このコードを使用してください:

public static void main(String[] args) { 
    String bosted=""; //Variable 
    String navn=""; //Variable 

    Scanner in = new Scanner(System.in); 

    System.out.println("Skriv inn navn: "); //What shows up when you first start the program 

    while(in.hasNext()) { //Only allow letters A-Z 
     navn = in.nextLine(); 
     if(!navn.isEmpty() && navn.matches("[A-Za-z]+")){ 
      System.out.println("Takk!"); //Says thank you if the user has entered letters 
      in.next(); 
      break; 
     } 
     else{ 
      System.out.println("Tall horer ikke hjemme i navn, prøv igjen!"); //Prints, "numbers dont belong in names, try again" if what the user entered is a number 
      in.next(); 
      System.out.println("Skriv inn navn: "); 
      continue; 
     } 

    } 

    System.out.println("Skriv inn bosted: "); 
    while(in.hasNext()) { //Only allow letters A-Z 
     bosted = in.nextLine(); 
     if(!bosted.isEmpty() && bosted.matches("[A-Za-z]+")){ 
     System.out.println("Takk!"); //Says thank you if the user has entered letters 
     break; 
     } 
     else{ 
      System.out.println("Tall horer ikke hjemme i navn, prøv igjen!"); //Prints, "numbers dont belong in names, try again" if what the user entered is a number 
      in.next(); 
      System.out.println("Skriv inn bosted: "); 
      continue; 
     } 


} 
    System.out.println("Hei, " + navn + "! Du er fra " + bosted + "."); //Prints out what the user has entered previously in a full sentence. 
} 
0

あなたは、本質的にループを再起動するcontinueキーワードを使用することができます。入力された文字列が空であるかの整数であれば、我々がチェックしている、と我々は、whileループは条件が失敗するまでの入力を求め続けることになります例外を投げることができるようif状態で

while(!in.hasNext("[A-Za-z]+")) { 
    try { 
     String s = in.nextLine(); 
     if(s.isEmpty() || s.matches("^-?\\d+$")){ 
      throw new Exception("empty string or number detected"); 
     } 
    } catch (Exception e){ 
     continue; 
    } 
} 

(つまりは、私たちを渡します。テスト)。

関連する問題