打印从1到用户指定的数字的整数。

huangapple go评论66阅读模式
英文:

Print the integers from 1 to a number given by user

问题

import java.util.Scanner;

public class FromWhereToWhere {

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

        // Write your program here
        System.out.println("Where to?");
        int userInput = Integer.valueOf(scanner.nextLine());
        int start = 1;

        while (start <= userInput) {
            System.out.println(start);
            start++;
        }
    }
}
英文:

I'm doing the Java MOOC by Helsinki University. Stuck on the following problem:

Write a program which prints the integers from 1 to a number given by the user.

Sample output

Where to? 3
1
2
3

The code below outputs the expected results but is not accepted as valid. Any suggestions or pointers are welcome, thank you!

    import java.util.Scanner;

    public class FromWhereToWhere {

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

        // Write your program here
        System.out.println(&quot;Where to?&quot;);
        int userInput = Integer.valueOf(scanner.nextLine());
        int start = 1;
       
        while (start &lt;= userInput) {
            System.out.println(start);
            start++;
        }
    }    
}

答案1

得分: 1

最有可能的是测试您的程序的系统正在将值插入标准输入(System.in),并假设您将使用.nextInt()进行读取。

如果不是这样,仔细检查程序说明;如果我输入-1会发生什么?0呢?1985985410395831490583440958230598呢?FOOBAR呢?

如果没有说明,那么验证器可能不会向您提供这些输入(如果确实提供了,请向MOOC提供者报告错误,如果是这种情况,课程本身需要修正),但如果提供了这些输入,您可能需要编写相应的规则。

这应该不是问题,但为了完全镜像所需的结果,它是System.out.print("Where to? "); - 注意,没有ln,以及尾随的空格。

英文:

Most likely the system that tests your program is stuffing values into standard input (System.in) with spaces, and assumes that you will read with .nextInt().

If that's not it, double check the program description; what is supposed to happen if I enter -1? 0? 1985985410395831490583440958230598? FOOBAR?

If it doesn't say, then presumably the verifier won't throw those inputs at you (if it does, file a bug with the MOOC provider, the course itself needs fixing if that is the case), but if it does, you're going to have to code those rules in, probably.

This shouldn't be it, but to exactly mirror the desired result, it's System.out.print(&quot;Where to? &quot;); - note, no ln, and a trailing space.

答案2

得分: 1

你没有检查用户输入是否有效,我建议从以下开始:

  1. 检查userInput是否为有效数字(包括数字字符)。
  2. 检查userInput是否大于或等于1。
英文:

You did not check if the user input is valid, I would suggest starting off with the following:

  1. check if userInput is a valid number (includes numeric characters).
  2. check if userInput is larger or equal to 1.

答案3

得分: 0

你的答案是可以的,但如果你的老师希望适用于初学者水平,那么可以进行优化,因为:

  1. 你可以直接从扫描器中获取一个整数,无需使用包装类Integer。
  2. 你可以使用另一个循环...一个for循环
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    // 在这里编写你的程序
    System.out.println("要去哪里?");
    int userInput = scanner.nextInt();

    System.out.println("好的!");
    for (int i = 1; i <= userInput; i++) {
        System.out.println(i);
    }
}
英文:

your answer is ok but can be optimized to the beginners levels if that is what your teacher is expecting because:

  1. you can get an int directly from scanner, no need to use the wrapper class Integer.
  2. you can use another loop ... a for loop
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    // Write your program here
    System.out.println(&quot;Where to?&quot;);
    int userInput = scanner.nextInt();

    System.out.println(&quot;ok!&quot;);
    for (int i = 1; i &lt;= userInput; i++)
    {
        System.out.println(i);
    }

}

答案4

得分: 0

只需尝试删除

System.out.println("Where to?");

改为

System.out.print("Where to?");
英文:

Just try by Removing
System.out.println("Where to?");
with
System.out.print("Where to?");

huangapple
  • 本文由 发表于 2020年9月3日 19:19:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63722561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定