Code Error | Tentang Basic I/O pada Java (Coding challenge on cody.tech website)

Coding challenge details :

Write a program that:

  1. prints "Enter your age:" (with new line)
  2. Gets input from the user, one integer (the user's age)
  3. calculates the difference between the user's age and 120. For example: for age 32, 88 will be calculated.
  4. prints the calculated number. The program goal is to calculate the number of years remaining till 120 years!

Pada website input akan di test dengan test cases sbg berikut : Test Cases Test #1 Input : 32 Expected output : 88

Test #2 Input : 20 Expected output : 100

Test #3 Input : 99 Expected output : 21

Test #4 (secret) *Pada website tidak diberitahu input seperti apa yg akan digunakan, seperti tantangan yang harus saya selesaikan dan temukan solusinya.

Saya kesusahan menemukan error atau kesalahan pada coding challenge ini. Saya coba jalankan di IDE kode program saya berjalan lancar, namun pada website ditemukan error seperti ini :

'The issue in your code is with the variable naming. In the second attempt, you declared the variable age twice, which is incorrect. Make sure to use different variable names for different values to avoid conflicts.'

Berikut kode saya dibawah ini:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your age: ");
        int age = scanner.nextInt();
        int res = 120 - age;
        System.out.println(res);
    }
}

Mungkin jika ada beberapa pakar yang tau ada pernah mengalami mohon bantuaannya.

avatar 233_037naufalihsansriyanto
@233_037naufalihsansriyanto

1 Kontribusi 0 Poin

Diperbarui 1 bulan yang lalu

1 Jawaban:

Jawaban Terpilih

hmm,,, coba tambah nextLine() setelah nextInt() karena biasanya nextInt ngebuat buffer,, jadi bisa membersihkan buffernya,,

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your age:");
        int age = scanner.nextInt();
        scanner.nextLine();  // coba tambah ini
        int res = 120 - age;
        System.out.println(res);
    }
}

kalo masi ga bisa, coba cek format input dan output di platform udh sesuai atau belum,, semoga membantu ^^

avatar yukaristel
@yukaristel

33 Kontribusi 31 Poin

Dipost 1 bulan yang lalu

Tanggapan

Thank you bantuannya.

Login untuk ikut Jawaban