循环和逻辑语句

时间:2015-11-16 22:19:06

标签: java loops boolean-logic

我有课程所需的这个程序,他们要求我完成它的方式令我感到困惑,因为我无法输出正确的答案。我们需要的是一系列包含一系列'x''X'和'r'的输入,它们又输出一个声音。如果输入包含的字符不是'x''X'或'r',则必须输出“请输入有效输入”的行。在大多数情况下,我把一切都搞砸了,但我无法找到正确显示无效输入字符串的方法。

import java.util.Scanner; 
public class String2Beat { //main class
public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    System.out.println("To play a drum song please enter a series of x's and r's."); 
    System.out.println("Use an Uppercase X for the base drum, "); 
    System.out.println("Use a Lowercase x for the snare drum, "); 
    System.out.println("Or use a Uppercase R for a rest:"); 
    String drums = scan.nextLine(); 

    for (int j = 0; j < 3; j++){
        for (int i = 0; i < drums.length(); i++){
            if (drums.charAt(i) != 'x' && drums.charAt(i) != 'X' && drums.charAt(i) != 'r'){
                System.out.println("not a valid string input");
            }
            else {
                if (drums.charAt(i) == 'X'){
                    System.out.println("Now playing a Bass Drum. " + drums.charAt(i)); 
                    playBass(); 
                }
                else if(drums.charAt(i) == 'x'){
                    System.out.println("Now playing a Snare Drum. " + drums.charAt(i)); 
                    playSnare(); 
                }
                else if(drums.charAt(i) == 'r'){
                    System.out.println("Now playing a Rest. " + drums.charAt(i)); 
                    playRest(); 
                }
            }

        }

    }

    scan.close();
}

1 个答案:

答案 0 :(得分:0)

您需要做的是在播放声音之前确定字符串是否包含无效输入。您的程序应该看起来像这样:

import java.util.Scanner; 
public class String2Beat { //main class
public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    System.out.println("To play a drum song please enter a series of x's and r's."); 
    System.out.println("Use an Uppercase X for the base drum, "); 
    System.out.println("Use a Lowercase x for the snare drum, "); 
    System.out.println("Or use a Uppercase R for a rest:"); 
    String drums = scan.nextLine(); 

    boolean test = false;

    for (int i = 0; i < drums.length(); i++)
    {
        if (drums.charAt(i) != 'X' || drums.charAt(i) != 'x' || drums.charAt(i) != 'R')
            test = true;
    }

    if (!test)
    {
        for (int j = 0; j < 3; j++)
        {
            for (int i = 0; i < drums.length(); i++)
            {
                    if (drums.charAt(i) == 'X')
                    {
                        System.out.println("Now playing a Bass Drum. " + drums.charAt(i)); 
                        playBass(); 
                    }
                    else if(drums.charAt(i) == 'x')
                    {
                        System.out.println("Now playing a Snare Drum. " + drums.charAt(i)); 
                        playSnare(); 
                    }
                    else
                    {
                        System.out.println("Now playing a Rest. " + drums.charAt(i)); 
                        playRest(); 
                    }
                }
            }
        }
    }
    else
    {
        System.out.println("not a valid string input");
    }

    scan.close();
}