Java从用户输入计数

时间:2016-09-04 18:51:08

标签: java

新Java学生和我正在为学校写作作业(第1周),我已经使程序工作大部分,但我希望它在输入有效数字时计入用户输入。(例如我喜欢1种方式,我喜欢2种方式,等等于输入数字)。一切都有效,除非我开始添加语句。感谢任何帮助。

这是我的代码:

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

        System.out.println("What is your name? ");
        name = scanIn.nextLine();
        System.out.println("Hello " + name);

        System.out.println("How much love do you need? (Enter a number from 0-10)");
        int love = scanIn.nextInt();
        String message;
        int line;
        if (love < 1) {
            message = "Everybody needs some love! " + name;
        } else if (love > 9) {
            message = "You can't handle that much love " + name;
        } else {
            for (love = 1; love <= line; love++) {
                message = "I love thee " + love + " ways";
            }
        }
        System.out.println(message);
        scanIn.close();
    }

2 个答案:

答案 0 :(得分:0)

代码似乎有一些问题,如@Andrew L.说有一个

  1. 多行括号的问题
  2. 您尚未初始化变量行或消息
  3. 由于线路没有被初始化,所以第三个被触及的人只会执行一次,因为线路为空并且爱情就是你输入的数字。
  4. 经过一些格式化后,我认为这大致是你想要实现的目标:

        public static void main(String[] args) {
            String name;
            try (Scanner scanIn = new Scanner(System.in)) {
                System.out.println("What is your name? ");
                name = scanIn.nextLine();
                System.out.println("Hello " + name);
    
                System.out.println("How much love do you need? (Enter a number from 0-10)");
                int love = scanIn.nextInt();
                String message = null;
                //int line=1;
                if (love < 1) {
                    message = "Everybody needs some love! " + name;
                } else if (love > 9) {
                    message = "You can't handle that much love " + name;
                } else {
                    for (int i = 1; i <= love; i++) {
                        message = "I love thee " + love + " ways";
                    }
                }
                System.out.println(message);
            }
        }
    }
    

    或者,如果您想循环并打印多行而不是在结尾处打印一次,请注意,如果条件允许,则仍需要将其他打印行移动到另一行中,或者将原件保留在外面打印之后:

    for (int i = 1; i <= love; i++) {
                        message = "I love thee " + love + " ways";
                        System.out.println(message);
                    }
    

    但for循环似乎仍然有点不必要,因为你已经知道了爱的价值所以你应该能够以常规方式将值连接到字符串而不需要循环。无论如何希望这对你的编程有所帮助,祝你好运!

答案 1 :(得分:0)

这可能会有所帮助:

import java.util.Scanner;
public class Counting {

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

System.out.println("What is your name? ");
name = scanIn.nextLine();
System.out.println("Hello " + name);

System.out.println("How much love do you need? (Enter a number from 0-10)");
int love = scanIn.nextInt();
 String message=null;

 if (love<1){
  message = "Everybody needs some love! " +name;
 }
   else if (love>9){
  message = "You can't handle that much love " +name;
   }
   else{
        for (int line=0;line<=love;line++)   
  message = "I love thee " + love + " ways";
   }
  System.out.println(message);
  scanIn.close();
  }
  }

理解你的问题有点困难。