Java循环,检查空字符

时间:2016-05-05 15:17:57

标签: java loops

import java.util.*;

public class java
{
private String m_name;
private String m_game;
private int m_score;
private int m_played;

public java( String name, String game, int score, int played)
{
    m_name = name;
    m_game = game;
    m_score = score;
    m_played = played;
}

public String getName()
{
    return m_name;
}

public String getgame()
{
    return m_game;
}

public int getscore()
{
    return m_score;
}

public int getplayed()
{
    return m_played;
}

public String toString()
{
    return "name: " + m_name + ", game: " + m_game + 
           ", Score: " + m_score + ", played: " + m_played;
}

public static void main(String[] args) 
{
   ArrayList<java> students = new ArrayList<java>();
   Scanner input = new Scanner(System.in);

   int menuChoice = 4;
   do {
       System.out.println("\t\t\tStudnent's Gaming Records");
       System.out.println("\t1. Add Student\t\t2. View Students \t3. Averages\t4. Exit");
       try {
           System.out.println("Enter a choice: ");
           menuChoice = Integer.parseInt(input.nextLine());
       } catch (NumberFormatException e) {
           continue;
       }

       if (menuChoice==1)
       {
           System.out.println("Player Name");
           String name = input.nextLine();

           System.out.println("Game:");
           String game = input.nextLine();

           int played = -1;
           do {
               try {
                   System.out.println("Time Played:");
                   played = Integer.parseInt(input.nextLine());
               } catch (NumberFormatException e) {
                   System.out.println("Enter a number!");
                   continue;
               }
           } while (played <= 0);

           int score = -1;
           do {
               try {
                   System.out.println("Score:");
                   score = Integer.parseInt(input.nextLine());
               } catch (NumberFormatException e) {
                   System.out.println("Enter a number!");
                   continue;
               }
           } while (score <= 0);


           java student = new java(name, game, score, played);
           students.add(student);
           System.out.println(student);

       } else if (menuChoice==2) {
           System.out.println("Students:");
           for (java student : students)
           {
               System.out.println(student);
           }
       }
   } while (menuChoice<3);
   System.out.println("swag");
}
}

这是我的代码到目前为止,它工作得非常好,但是我很难先尝试弄清楚一些事情,当用户按下1时我想要游戏条目,得分条目并且无限期地循环播放条目直到当用户输入“退出”时,在我的菜单的“2”部分中,它将显示用户在一个用户名下输入的所有游戏,分数和时间。我的第二个问题是如何检查空字符串?提前全部谢谢。

1 个答案:

答案 0 :(得分:0)

我相信这是一个功课,所以我不应该给出确切的答案。

对于第一个问题,使用while循环,如下所示:

String userEntry = "1";
while(userEntry.equals("1")){
    //yourCode
}

对于第二个问题,使用.equals如下:

if ("".equals(scannedString)){
   //do something
}else{
   // your code
}