Header Ads

[Basic] Một số bài tập Java cơ bản về biến, hằng số, lệnh if và switch

  1. Variables: 

    a. Declare a variable called "age" and assign it the value 30. 
    b. Declare two variables called "width" and "height" and assign them the values 10 and 20 respectively. Calculate their product and store the result in a new variable called "area".
    c. Declare a variable called "name" and assign it the value "John". Declare a second variable called "age" and assign it the value 25. Print the message "My name is John and I am 25 years old" to the console.
    d. Declare a variable called "number" and assign it the value 10. Increment the value by 1 using the increment operator and print the result to the console.
    e. Declare a variable called "price" and assign it the value 19.99. Declare a second variable called "taxRate" and assign it the value 0.07. Calculate the total cost of the item (including tax) and store it in a new variable called "totalCost".
    f. Declare a variable called "message" and assign it the value "Hello, world!". Print the message to the console.
  2. If statements: 

    • a. Write an if statement that checks if a variable called "num" is greater than 10. If it is, print "num is greater than 10". Otherwise, print "num is less than or equal to 10". 
    • b. Write an if statement that checks if a variable called "isRaining" is true. If it is, print "Remember to bring an umbrella". Otherwise, print "Enjoy the sunshine!".
    • c. Write an if statement that checks if a variable called "isPositive" is true. If it is, print "The number is positive". If it is false, print "The number is negative".
    • d. Write an if statement that checks if a variable called "password" is equal to "letmein". If it is, print "Access granted". If it is not, print "Access denied".
    • d. Write an if statement that checks if a variable called "isEven" is true. If it is, print "The number is even". Otherwise, print "The number is odd".
    • e. Write an if statement that checks if a variable called "age" is less than 18. If it is, print "You are not old enough to vote". Otherwise, print "You are eligible to vote".
    • f. Write a program that prompts the user to enter a character and then prints "Vowel" if the character is a vowel and "Consonant" if the character is a consonant. 
    • g. Write a program that prompts the user to enter a number and then prints "Even" if the number is even and "Odd" if the number is odd.
    • h. Write a program that prompts the user to enter a year and then prints "Leap Year" if the year is a leap year and "Not a Leap Year" otherwise.
    • k. Write a program that prompts the user to enter three numbers and then prints the largest of the three numbers.
  3. Switch statements: 

    • a. Write a switch statement that takes a variable called "dayOfWeek" and prints "Monday" if the value is 1, "Tuesday" if the value is 2, and so on up to "Sunday" if the value is 7. 
    • b. Write a switch statement that takes a variable called "grade" and prints "Excellent" if the value is 90 or above, "Good" if the value is between 80 and 89, "Fair" if the value is between 70 and 79, and "Poor" if the value is below 70.
    • c. Write a switch statement that takes a variable called "day" and prints "Weekday" if the value is between 1 and 5, and "Weekend" if the value is between 6 and 7.
    • d. Write a switch statement that takes a variable called "language" and prints "English" if the value is 1, "French" if the value is 2, "Spanish" if the value is 3, and "Unknown" if the value is not 1, 2, or 3.
    • e. Write a switch statement that takes a variable called "direction" and prints "North" if the value is 1, "East" if the value is 2, "South" if the value is 3, and "West" if the value is 4.
    • f. Write a switch statement that takes a variable called "grade" and prints "A" if the value is between 90 and 100, "B" if the value is between 80 and 89, "C" if the value is between 70 and 79, "D" if the value is between 60 and 69, and "F" if the value is below 60.
    • g. Write a program that prompts the user to enter a month (as a string) and then prints the corresponding number of days in that month. Handle leap years appropriately (February has 29 days in leap years, 28 in non-leap years).
    • h. Write a program that prompts the user to enter a letter (as a character) and then prints "Uppercase" if the letter is an uppercase letter and "Lowercase" if the letter is a lowercase letter. 
    • i. Write a program that prompts the user to enter a year and a month (as integers) and then prints the number of days in that month for that year. Handle leap years appropriately.
    • k. Write a program that prompts the user to enter a letter (as a character) and then prints "Vowel" if the letter is a vowel, "Consonant" if the letter is a consonant, or "Not a Letter" if the character is not a letter.
  4. Constants: 

    • a. Declare a constant called "PI" and assign it the value 3.14159. Use it to calculate the circumference of a circle with a radius of 5. 
    • b. Declare a constant called "MAX_VALUE" and assign it the value 100. Write an if statement that checks if a variable called "num" is greater than MAX_VALUE. If it is, print "num is too large". Otherwise, print "num is within the acceptable range".
    • c. Declare a constant called "SPEED_OF_LIGHT" and assign it the value 299792458. Use it to calculate the energy of a photon with a frequency of 6.0 × 10^14 Hz.
    • d. Declare a constant called "MIN_AGE" and assign it the value 18. Write an if statement that checks if a variable called "userAge" is greater than or equal to MIN_AGE. If it is, print "You are old enough to vote". If it is not, print "You are not old enough to vote".e. Declare a constant called "G" and assign it the value 9.81. Use it to calculate the force of gravity on a 10 kg object.
    • f. Declare a constant called "TAX_RATE" and assign it the value 0.08. Write an if statement that checks if a variable called "subtotal" is greater than 100. If it is, calculate the total cost (including tax) and print it to the console. If it is not, print the subtotal.
     

    Another basic exercises:

    1. Hello World:

    a. Write a program that prints the message "Hello, world!" to the console.
    b. Write a program that prints the message "My name is _____" (fill in the blank with your name) to the console.

    2. Variables:

    a. Declare a variable called "age" and assign it the value 25. Print the value to the console.
    b. Declare a variable called "balance" and assign it the value 1000. Add 500 to the value and print the result to the console.

    3. Input and output:

    a. Write a program that prompts the user to enter their name and then prints the message "Hello, _____" (fill in the blank with the user's name) to the console.
    b. Write a program that prompts the user to enter two numbers and then prints their sum to the console.

    4. Arithmetic operators:

    a. Write a program that calculates and prints the area of a rectangle with a width of 5 and a height of 10.
    b. Write a program that calculates and prints the value of 10 divided by 3 (use floating-point division).
     
     
     

Không có nhận xét nào

Được tạo bởi Blogger.