Skip to content
View in the app

A better way to browse. Learn more.

Komuniti @PuTeRA

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Help] Switch Case + Char

Featured Replies

Assalamulaikum salam sejahtera..
saya baru lagi belajar java... saya belajar switch case ni boleh ambil nilai string dan integer.
tapi bagaimna pula kalau char?

ni snippet saya..
[code]import java.io.*;
public class Tugas2
{
public static void main(String args[])throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

//char pilihan;

System.out.println("========================================\n");
System.out.print(" ~BASIC MATHEMATICAL CALCULATION JAVA~\n");
System.out.print("\n========================================\n");
System.out.println("CODE\t\tCALCULATION");
System.out.println("====\t================================\n");
System.out.print("> A\t Circumference of circle.\n");
System.out.print("> B\t Circumference of square.\n");
System.out.print("> C\t Circumference of triangle.\n");
System.out.print("> D\t Odd or even number.\n\n");
System.out.print("========================================\n");
System.out.println("Instruction:");
System.out.print("\tEnter the CODE given at above to");
System.out.print("\n\tcalculate to operate the program.");
System.out.print("\n========================================\n");
System.out.print("\n# Code : ");
char pilihan = stdin.readLine();

switch(pilihan)
{
case 'a':
case 'A': System.out.print("test A");
break;
case 'b':
case 'B': System.out.print("test B");
break;
case 'c':
case 'C': System.out.print("test C");
break;
case 'd':
case 'D': System.out.print("test D");
break;
default: System.out.print("lol..wrong");
}
}
}
[/code]

bila kumpil kuar error..
[code]D:\>javac Tugas2.java
Tugas2.java:25: incompatible types
found : java.lang.String
required: char
char pilihan = stdin.readLine();
^
1 error

D:\>
[/code]

Ibarat, kod saya buat ni akan baca format string kan ? (teka je)..
string ada 1 @ lebih dari 1 huruf... tapi saya nakkan java ni hanya pada 1 huruf je..
mcmna nak betulkan msalah ni ya?

*soalan tambahan:
- boleh ke nak lower case input? supaya saya taknyah nak case a & case A..
  • Author
ha'ah..pusing. x3. setel sendiri
[code]char pilihan = stdin.readLine().charAt(0);[/code]
selesai msalah......
Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.