需要帮助修复此程序

时间:2015-06-25 17:23:01

标签: java

我应该如何编写程序以便它接受用户的输入并将其分配给变量年龄然后运行代码?

public class Callone 
{
    public void print_det()
    {
        int age = y;
        if(age > 25)
            System.out.println("Not Valid");
        else
            System.out.println("Valid");
     }
    public static void main(String[] args)throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
        System.out.println("Enter the age of the applicant");
        int y = Integer.parseInt(br.readLine());
        Callone c = new Callone();
        c.print_det();
    }
}

2 个答案:

答案 0 :(得分:2)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Callone {

    public void print_det(int y)
    {
        int age = y;
        if(age > 25)
        System.out.println("Not Valid");
        else
            System.out.println("Valid");
    }
    public static void main(String[] args)throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the age of the applicant");
        int y = Integer.parseInt(br.readLine());
        Callone c = new Callone();
        c.print_det(y);
    }
}

在eclipse中运行时,控制台在停止输入数字的用户输入类型并点击输入时你应该看到输出

答案 1 :(得分:0)

我无法在那里看到您的y变量:

int age = y;

您必须使用方法参数传递它:

public class Callone 
{
    public void print_det(int y)
    {
        int age = y;
        if(age > 25)
            System.out.println("Not Valid");
        else
             System.out.println("Valid");
    }
    public static void main(String[] args)throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
        System.out.println("Enter the age of the applicant");
        int y = Integer.parseInt(br.readLine());
        Callone c = new Callone();
        c.print_det(y);
    }
}

让它变得更好"甜蜜":

public class Callone 
{
    public void print_det(int age)
    {
        if(age > 25)
            System.out.println("Not Valid");
        else
             System.out.println("Valid");
    }
    public static void main(String[] args)throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
        System.out.println("Enter the age of the applicant");
        int age = Integer.parseInt(br.readLine());
        Callone c = new Callone();
        c.print_det(age);
    }
}

未经测试。