0
Is there Syntax ErroR.
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String name = read.nextLine(); int age = read.nextInt(); Student student = new Student(); student.name = name; student.setAge(age); System.out.println("Name: " + student.name); System.out.println("Age: " + student.getAge()); } } class Student { public String name; private int age; public int getAge() { return age; } public void setAge(int age) { if(age<0) {System. out.println("Invalid age");this.age=0;} } }
3 Antworten
+ 3
Run it in Code Playground. You'll see if there are errors, which ones, and in which lines.
If you need help in the code, edit your question and include a link to your code in Code Playground and a description of your difficulties. This avoids an endless loop of questions to understand just that.
+ 1
There is no syntax error, but take a look at your age setter 😉
0
age is never set to Student.age field