עבור לתוכן
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.

עזרה| שאלה ב-JAVA למתחילים

Featured Replies

פורסם

אני מנסה לפתור את השאלה הזו אבל לא ממש הצלחתי , מקווה שתוכלו לעזור לי בה.

1. כתוב פעולה רקורסיבית המקבלת מספר שלם ומחזירה מספר שבו כל ספרה אי-זוגית תוחלף בספרה קטנה ממנה באחד.

כלומר אם התוכנית מקבלת : 3378 אז היא תחזיר : 2268 (מקבלת: 3968 מחזירה: 2868 )

מה שניסיתי בינתיים זה זה :

אין ממש "כתובת קוד כזו כי אני לא יודע איך לשים כזה דבר אז העתקתי הכול :

import java.util.*;

public class halfsimilar {

public static void main(String[] args)

{ Scanner reader = new Scanner(System.in);

int num = 3378 ;

System.out.println(peoola(num , 0));

}

public static int peoola (int num , int digits)

{

if(num<10)

{

if(num%2==1)

return (num-1) ;

else

return num ;

}

else

{

if((num%10)%2==1)

return ((num%10)-1)*(digits*10) + peoola(num/10 , digits+1) ;

else

return ((num%10))*(digits*10) + peoola(num/10 , digits+12) ;

}

}

פורסם

תעביר את הקוד שלך לתצוגת קוד (אייקון הרשתית)

פורסם

	public static void main(String[] args) {
System.out.print("enter your number: ");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
System.out.println("\n" + calculate(number));
}


private static int calculate(int number) {
if (number < 10)
if (number % 2 == 0)
return number;
else
return --number;
else
if (number % 2 == 0)
return (number % 10) + 10 * (calculate(number / 10));
else
return ((number % 10) - 1) + 10 * (calculate(number / 10)); }

היית בכיוון עם לבודד את ספרת האחדות זה היה הטריק בשאלה, תרגיל נחמד,

אם תחליף את המספר 10 במשתנה base תקבל פתרון כללי ולא רק לבסיס 10

פורסם

בכל מקרה רצוי לא להשתמש ב-magic numbers אלא בקבועים עם שמות מייצגים.

ארכיון

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

דיונים חדשים

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.