עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

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.

שגיאה קומפילציה בלולאת While בJAVA

Featured Replies

פורסם


import java.util.Scanner;
public class Car{
public static void main (String [] args)
{
System.out.println ("can I get the car?");
Scanner scan = new Scanner (System.in);

int x;

x = scan.nextInt();

while (x != "yes") {
system.out.println ("try again");
x = scan.nextInt();
}

System.out.println ("last go");
}
}}

השגיאה שמתקבלת:

incompareable type: int and java.lang.string

שורה 8 מלמטה זאת השורה הבעייתית:

  while (x i= yes) {

המטרה שהלולאה תרוץ כל עוד X לא מקבל "yes"

פורסם

x = int

yes = string

אתה לא יכול להשוות ביניהם

פורסם
  • מחבר

אז מה עליי לשנות?

שיניתי מINT לSTRING X

ושיניתי את SCAN.next int ל:

x = scan.nextString;

השורה הבעייתי כרגע היא:

  while (x !equals "YES") {

השגיאה:

'(' expected

הקוד נראה כך:

import java.util.Scanner;
public class Car{
public static void main (String [] args)
{
System.out.println ("can I get the car?");
Scanner scan = new Scanner (System.in);

String x;

x = scan.nextString;

while (x !equals "YES") {
System.out.println ("try again");
x = scan.nextInt();
}

System.out.println ("last go");
}
}

פורסם

תעלה את כל הקוד המתוקן. הרי אם שינית את x ל-string, אתה לא יכול לקרוא לתוכו עם nextInt...

חוץ מזה:

א. זה String, לא string.

ב. מחרוזות משווים באמצעות equals ולא באמצעות == או =!

פורסם
  • מחבר

תוקן סופית.

פורסם

א. equals זו פונקציה, לא אופרטור. חפש קצת בגוגל איך אמורים להשתמש בה.

ב. זו לא השורה הבעייתית היחידה. הוא אפילו אומר לך שחסרים לך סוגריים.

פורסם
  • מחבר

שניצל - אלף תודות.

עובד פיקס. אני יתחיל להשתמש יותר בגוגל לפני שאני בא לפה.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

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.