Java 需要帮助修复此程序吗
我应该如何编写程序,以便它接受用户的输入并将其分配给可变年龄,然后运行代码Java 需要帮助修复此程序吗,java,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 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();
}
}
我看不到您的
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);
}
}
为了让它更“甜美”:
没有测试
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中运行时,控制台在暂停用户输入的同时键入数字并按enter键,您应该会看到输出您在上面的代码中遇到了什么问题?
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);
}
}