为什么我的程序会被编译?

时间:2014-09-13 01:30:41

标签: java string loops

import java.util.Scanner;

public class CodeSnippetm01 {
    public static void main(final String[] args, int len) {
        //0. Variable declarations.
        String greeting = "Welcome to CST242!";
        String question1 = "What is your name?:";

        //1. Print Statement.

        System.out.println(greeting);
        System.out.println(question1);

        Scanner keyboard = new Scanner(System.in);
        len = input.length();
        //2. Code for prompt and input
        while (len > 0) {

            String input = keyboard.nextLine();

            System.out.println("String Length is : " + len);

            //3. Loop code and loop body.
        }
        //4. Multiway If block.
    }
}

我必须创建一个循环,在人名的每一行上显示一个字符,使用Scanner方法输入一个表示该人年龄的整数,然后使用多路if-else语句打印以下内容(显示在Pseudocode中):

If the person is >= 90, print `({name from 2} +“was born before 1923.”)`. 

Else if person >=75, print `({name from 2} +“was born born before 1938.”)`. 

Else if person >=50, print `({name from 2} +“was born born before 1963.”)`. 

Else if person >=25, print `({name from 2} +“was born born before 1988.”)`. 

Else print `({name from 2} +“was born born sometime after 1988.”)`

到目前为止,这就是我所拥有的......欢迎任何建议或帮助: - )

1 个答案:

答案 0 :(得分:1)

input在使用之前未定义。使用以下代码重试。

import java.util.Scanner;

public class CodeSnippetm01 {

    public static void main(String[] args, int len) {
        //0. Variable declarations.
        String greeting = "Welcome to CST242!";
        String question1 = "What is your name?:";

        //1. Print Statement.
        System.out.println(greeting);
        System.out.println(question1);
        Scanner keyboard = new Scanner(System.in);
        String input = keyboard.nextLine();
        len = input.length();
        //2. Code for prompt and input
        while (len > 0) {
            input = keyboard.nextLine();
            System.out.println("String Length is : " + len);
            //3. Loop code and loop body.
        }
        //4. Multiway If block.
    }
}