Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 输入循环不使用整数_Java_Loops - Fatal编程技术网

Java 输入循环不使用整数

Java 输入循环不使用整数,java,loops,Java,Loops,我一直在这方面的工作和搜索这么长时间。我对此非常陌生(一周内),我似乎不知道如何循环(做/当等…)月份或颜色输入。我只能找到关于int的循环帮助。我读到我可能必须转换为int。如果这个问题太模糊,很抱歉。这是一门课,我正在努力学习,所以如果你不介意的话,也可以简单地解释一下 package FortuneTeller; import java.util.Scanner; public class FortuneTeller { private static Scanner inpu

我一直在这方面的工作和搜索这么长时间。我对此非常陌生(一周内),我似乎不知道如何循环(做/当等…)月份或颜色输入。我只能找到关于int的循环帮助。我读到我可能必须转换为int。如果这个问题太模糊,很抱歉。这是一门课,我正在努力学习,所以如果你不介意的话,也可以简单地解释一下

package FortuneTeller;

import java.util.Scanner;

public class FortuneTeller {

    private static Scanner input;

    public static void main(String[] args) {
        input = new Scanner(System.in); 

        System.out.println("Hello human. What is your first name?");
        String name = input.nextLine();

        System.out.println("And your last?");
        String name2 = input.nextLine();

        System.out.println(name + " "+ name2 + "? Interesting. What is your age?");
        int age = input.nextInt();

        System.out.println("What number earth month were you born in?");
        int month;
        **month = input.nextInt();**
        switch (month) {
            case 1:
                System.out.println("January");
                break;
            case 2:
                System.out.println("February");
                break;
            case 3:
                System.out.println("March");
                break;
            case 4: 
                System.out.println("April");
                break;
            case 5:
                System.out.println("May");
                break;
            case 6:
                System.out.println("June");
                break;
            case 7:
                System.out.println("July");
                break;
            case 8:
                System.out.println("August");
                break;
            case 9:
                System.out.println("September");
                break;
            case 10:
                System.out.println("October");
                break;
            case 11:
                System.out.println("November");
                break;
            case 12:
                System.out.println("December");
                break;
            default:
                System.out.println("This is no Earth month. What EARTH month were you born in?");
                break;
        }

        System.out.println("Favorite ROYGBIV color. Type \"Help\" if you are unsure of the reference.");
        String color;
        **color = input.next();**
        switch (color.toLowerCase()) {
                case "red":
                System.out.println("What is your number of siblings?");
                break;
            case "orange":
                System.out.println("What is your number of siblings?");
                break;
            case "yellow":
                System.out.println("What is your number of siblings?");
                break;
            case "green":
                System.out.println("What is your number of siblings?");
                break;
            case "blue":
                System.out.println("What is your number of siblings?");
                break;
            case "indigo":
                System.out.println("What is your number of siblings?");
                break;
            case "violet":
                System.out.println("What is your number of siblings?");
                break;
            case "help":
                System.out.println("ROYGBIV consist of Red. Orange. Yellow. Green. Blue. Indigo. Violet." +"\n"
                + "What is your favorite ROYGBIV color?");
                break;
            default:
                System.out.println("This is an invalid color. Type \"Help\" if you are unsure of the reference. "); 
        }

        int siblings = input.nextInt();

        System.out.println("Thank you!" + "\n" 
            + name + name2 );

        if (age %2==0) {
            System.out.println("You will hit the lottery in the next 10 years.");
        } else; {
            System.out.println("You will come into a high paying, enjoyable job in the next 5 years.");
        }

        if (siblings ==1) {
            System.out.println("You will live in a new place every 5 years.");
        }

        if (siblings ==2) {
            System.out.println("You will live in your home town and make it a better place with your resources.");
        }

        if (siblings ==3) {
            System.out.println("You and your three siblings will live successfully in a major city abroad.");
        }

        if (siblings >4) {
            System.out.println("You will start a comunie with all of your family in a beautiful remote place.");
        } else {
            System.out.println("An unexspected surprise will land you in a beautiful forign city with everything you need.");
        }

        switch (color) {
            case "Red" :
                System.out.println("You will travel by car");
                break;
            case "Orange" :
                System.out.println("You will travel by hot air balloon.");
                break;
            case "Yellow" :
                System.out.println("You will travel by plane");
                break;
            case "Green" :
                System.out.println("You will travel by train");
                break;
            case "Blue" :
                System.out.println("You will travel by boat");
                break;
            case "Indigo" :
                System.out.println("You will travel by submarine");
                break;
            case "Violet" :
                System.out.println("You will travel by big rig");
        }

        if ((month ==1) || ( month ==2) || (month ==3) || (month==4)) {
            System.out.println("Consider taking more time to read");
        } else if ((month ==5) || (month ==6) || (month ==7) || (month ==8)) {
            System.out.println("Consider taking on a mechanical hobby");
        } else if ((month ==9) || (month ==10) || (month ==11) || (month ==12)) {
            System.out.println("Consider taking on an artistic hobby");
        } else {
            System.out.println("Continue to attract atoms. The culmination will be worth wile.");
        }
    }
}
啊,你的澄清“循环每个部分,以防输入无效”让你的问题更清楚了。您将需要以下内容:

// declare & init a boolean variable "monthIsValid" to false
while (!monthIsValid) {
    // ...the code to ask for the month and print it, like you already have
    }
除此之外,在循环中,您需要在12种情况中的每种情况下将
monthIsValid
设置为
true
。(这是非常重复的;你可以“偷偷摸摸”,在你要求之前将
monthIsValid
设置为
true
,然后只有在
默认值
/无效的情况下,你才能返回并将其设置为
false


要知道,这个程序分配有点枯燥。特别是,我几乎不需要
开关
语句;我觉得
if
-
else
-
if
更适合学习者(因为这是一个基本的结构,会为你服务很多次)。有些人建议,一旦你了解了数组,就有更简单的方法来解决这个问题(所以不要觉得编程本身就是重复的)。另外,在我的Java教学中,我关注的是有单独的函数(因此我可能会要求一个单独的函数
colorMessage
,它接收一个字符串并返回一个字符串,或者如果没有合适的消息,则返回一个sentinel值)。这将把这个杂乱无章的大问题分解成更好的、自包含的块。

已经多年没有使用Java了,所以我现在不是什么语法错误,而是
};{
和*****color=input.nextInt();***以及*****color=input.next();***`(可能只是为了突出显示,不确定)是的,这些只是为了突出我试图循环的部分。谢谢!嗨,Antonio,你在那里提供了很多代码。你能编辑到a并指出到底出了什么问题吗?我不知道你到底想用循环做什么/为什么。非常感谢你!我真的很感谢你的时间和解释。我有点理解,但我不知道如何将其放入我的代码中。不过这很好。我觉得这可能是一个愚蠢的问题。我觉得好像我真的不知道如何提问。再次感谢您的时间。