I get an error saying "The left-hand side of an assignment must be a variable" where is says else (itemNumber >=15)
import java.util.Scanner;
public class Ch3Asg
{
public static void main(String[] args)
{
// Variables
Scanner input = new Scanner(System.in);
int itemNumber = 0;
double shippingCost = 0;
// Items Purchased
System.out.println("How many items did you purchase? ");
itemNumber = Integer.parseInt(input.nextLine());
// One Item Purchased
if ( itemNumber == 1 )
{
shippingCost = 2.99;
}
// 2-5 Items Purchased
else if ( itemNumber >= 2 && itemNumber <= 5 );
{
shippingCost = 2.99 + 1.99 * (itemNumber - 1);
}
// 5-15 Items Purchased
if ( itemNumber > 5 && itemNumber < 15)
{
shippingCost = 2.99 + 1.99 * (itemNumber - 1) + 1.49 * (itemNumber - 5);
}
// More Than 15 Items Purchased
else ( itemNumber >= 15 )
{
shippingCost = 2.99 + 1.99 * (itemNumber - 1) + 1.49 * (itemNumber - 5)
+ .99 * (itemNumber - 14);
}
// Display Cost
System.out.printf("Shipping Cost is: $%.2f", shippingCost);
}
}
else (boolean statement) is meaningless. I think that you forgot the if:
else ( itemNumber >= 15 )
should be
else if ( itemNumber >= 15 )
Or else if it represents the last and default option, it could be simply:
else {
//..
}
else construct does not take an expression, so
else ( itemNumber >= 15 )
is syntactically wrong.
You need to use else if instead
else if ( itemNumber >= 15 )
Related
I have a lottery program that will run bets and randomly generate numbers. When the user reaches $5000 in total wins, that user will have an option to cash out $5000 and use the remaining balance to continue the loop. The user will also be able to end the loop if choosing to not cash out. How do I continue a loop based on user input? If the user writes yes, the loop will continue, but I am not sure how to do that. Can someone please help? If the loop is supposed to continue, $5000 removed from the user's total win because of cashout, the remaining balance will be used to play.
I have tried putting the if statement for user input in multiple places in the main loop, but the user budget keeps increasing, it does not subtract $5000.
java.util.Scanner
while (budget > 0) {
lottery = (int) (Math.random() * 100);
guess = (int) (Math.random() * 100);
budget -= 1; // budget = budget-1;
// Get digits from lottery
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
System.out.println("The computer picked number is " + guess);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win $10,000");
totalWin += 1000;
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
System.out.println("Match all digits: you win $3,000");
totalWin += 300;
} else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2) {
System.out.println("Match one digit: you win $1,000");
totalWin += 100;
} else
System.out.println("Sorry, no match");
budget += totalWin;
totalWin=0;
if (budget > initBudget)
if ((budget-initBudget) >= 5000) {
System.out.println("You have reached the goal of 5000, we can go home!");
System.out.println("Enter 1 if you would like to cash out and use remaining balance to play and 0 if you would like to stop");
int decison = input.nextInt();
if (decison==1){budget=budget-5000;
System.out.println("Budget After play " + budget);
continue;
}break;
}
System.out.println("Budget After play " + budget);
}
So in my code, I have the loop
System.out.println("Enter 1 if you would like to cash out and use remaining balance to play and 0 if you would like to stop");
int decison = input.nextInt();
if (decison==1){budget=budget-5000;
System.out.println("Budget After play " + budget);
continue;
}break;
}
This loop should receive user input and then subtract 5000 from the total win and use the remaining budget to continue play.. but it is only increasing. Don't understand why it is increasing if I have -5000.
(if possible, please help me change the answer to a yes/no rather than input 1)
I assumed you are using java.util.Scanner. Here is the answer;
System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you " +
"would like to stop");
String decison = input.nextLine();
if ("yes".equals(decison))
{
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break; // break the statement.
Erçin Akçay explained how to change the input from an int to a String.
I took a look at your code and made a runnable main method using it, and it works as you would expect. Perhaps the fact that it needs to be 5k from the intial budget is confusing?
if ((budget - initBudget) >= 5000)
Here is the entire class I used to check your code if it is helpful:
import java.util.Scanner;
public class Test {
private static int budget;
private static int lottery;
private static int guess;
private static int totalWin;
private static int initBudget = 4500;
public static void main(String[] args) throws InterruptedException {
Scanner input = new Scanner(System.in);
budget = initBudget;
while (budget > 0) {
lottery = (int) (Math.random() * 100);
guess = (int) (Math.random() * 100);
budget -= 1; // budget = budget-1;
// Get digits from lottery
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
System.out.println("The computer picked number is " + guess);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win $10,000");
totalWin += 1000;
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
System.out.println("Match all digits: you win $3,000");
totalWin += 300;
} else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2) {
System.out.println("Match one digit: you win $1,000");
totalWin += 100;
} else {
System.out.println("Sorry, no match");
}
budget += totalWin;
totalWin = 0;
if (budget > initBudget) {
if ((budget - initBudget) >= 5000) {
System.out.println("You have reached the goal of 5000, we can go home!");
System.out.println(
"Enter 1 if you would like to cash out and use remaining balance to play and 0 if you "
+ "would like to stop");
int decison = input.nextInt();
if (decison == 1) {
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break;
}
}
System.out.println("Budget After play " + budget);
}
}
}
You can change your code like this:
System.out.println("Enter yes if you would like to cash out and use remaining balance to play and no if you would like to stop");
String decision = input.next();
if ("Yes".equalsIgnoreCase(decision)) { // Cashing Out
budget = budget - 5000;
System.out.println("Budget After play " + budget);
continue;
}
break; // breaking the loop (if user enters anything other than yes)
Here I have used String (to store User's response as a word). If he enters Yes, yes or YES (ignoring the case, it will be considered that user wants to cash out) the loop will not break.
The question says The Fast Freight Shipping Company charges the following rates:
Weight of Package Rate per 500 Miles Shipped
2 pounds or less $1.10
Over 2 pounds but not more than 6 pounds $2.20
Over 6 pounds but not more than 10 pounds $3.70
Over 10 pounds $3.80
The shipping charge per 500 miles are not prorated. For example, if a 2-pound package is shipped 550 miles, the charges would be $2.20. Write a program that asks the user to enter the weight of a package and then displays the shipping charges.
My problem is that I keep receiving two different answers everytime I put in a weight and distance. For example when I enter the weight as 2 pounds and the distance as 500 miles I get the answers $0.0 and $3.8 which are both incorrect answers. It looks like some weights that I enter are correct answers and others I enter give me incorrect answers. Heres my program:
//import java utilities for scanner class
import java.util.Scanner;
public class ShippingCharge
{
public static void main (String[] args)
{
//Declare and initialize variable to hold the entered weight.
int weight = 0;
//Declare and initialize variable to hold the entered distance.
double distance = 0.0;
//This variable will hold the calculated rate.
double rate;
//This will decide if the shipping charge will advance up one level.
int distanceMultiplier = (int)distance / 500;
//This will hold the increments of the shipping charge.
int distanceRemainder;
//Create a Scanner object for the input.
Scanner input = new Scanner(System.in);
//Get the weight of the package.
System.out.println("What is the weight of the package (in pounds)?");
weight = input.nextInt();
//Get the shipping distance of the package.
System.out.println("What is the shipping distance (in miles)?");
distance = input.nextDouble();
distanceRemainder = (int)distance % 500;
if (distanceRemainder == 0)
{
if (weight <= 2)
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 1.10));
}
else if (weight > 2 && weight <= 6)
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 2.20));
}
else if (weight > 6 && weight <= 10)
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 3.70));
}
else
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 3.80));
}
if (distanceRemainder != 0)
{
if (weight <= 2)
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 1.10);
}
else if (weight > 2 && weight <= 6)
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 2.20);
}
else if (weight > 6 && weight <= 10)
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 3.70);
}
else
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 3.80);
}
//end program
System.exit(0);
}//end main
}//end class
This will work for you
public static void main(String[] args) {
int weight = 0;
double distance = 0.0 , distanceExtra ;
Scanner in = new Scanner(System.in);
System.out.println("Weight ? ");
weight = in.nextInt();
System.out.println("Distance ? ");
distance = in.nextDouble();
distanceExtra = distance / 500;
distanceExtra = Math.ceil(distanceExtra);
if (weight <= 2) {
System.out.printf("charge is :" , (distanceExtra * 1.10));
}
else if (weight > 2 && weight <= 6)
{
System.out.printf("charge is :" , (distanceExtra * 2.20));
}
else if (weight > 6 && weight <= 10)
{
System.out.printf("charge is :" , (distanceExtra * 3.70));
}
else if (weight > 10)
{
System.out.printf("charge is :" , (distanceExtra * 4.80));
}
}
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
int weight = 0;
double distance = 0.0 ;
Scanner keyboard =new Scanner(System.in);
System.out.println("Enter the Distance");
distance = keyboard.nextDouble();
System.out.println("Enter the Weight");
weight = keyboard.nextInt();
if (weight <= 2) {
System.out.println("charge is : " + "$"+1.10);
}
else if (weight > 2 && weight <= 6)
{
System.out.println("charge is : " + "$"+2.20);
}
else if (weight > 6 && weight <= 10)
{
System.out.println("charge is : " + "$"+3.70);
}
else if (weight > 10)
{
System.out.println("charge is :" + "$"+4.80);
}
}
}
I don't understand why only my while statement is working and it does not move on to the for statement for the valid integer.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
long posNumber;
long x;
long fact = 1;
do {
System.out.print("Enter a number between 2 and 15: ");
Scanner in = new Scanner(System.in);
posNumber = in.nextLong();
} while (posNumber >= 2 || posNumber <= 15);
for (x = 1; x <= posNumber; x++)
fact = fact*x;
System.out.println("Factorial of " +posNumber+ " is " +fact);
}
}
You should try something like, if you plan to get numbers in a loop:
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter a number between 2 and 15: ");
posNumber = in.nextLong();
for (x = 1; x <= posNumber; x++)
fact = fact*x;
System.out.println("Factorial of " +posNumber+ " is " +fact);
}
} while (posNumber >= 2 || posNumber <= 15);
Or you can change the condition (in case to run it just once):
while (posNumber < 2 || posNumber > 15);
You want your program to continue asking the user if the number is invalid. That means if it is less than 2 or greater than 15. Replace your while condition with:
do {
...
} while (posNumber < 2 || posNumber > 15);
If the user enters a 1, posNumber < 2 will evaluate to true causing the loop to repeat and ask for a new number.
If the user enters 3, both posNumber < 2 and posNumber > 15 will evaluate to false and the loop will break and then your for loop will execute.
My code compiles correctly. When I run my program the output asks me to enter weight of package. If I enter a negative number the program asks me to enter weight again, but won't stop to let me enter another number.
I think the problem is in the "while" statement but I'm not sure.
Any help would be appreciated.
import java.util.Scanner;
public class dcrawford_Shipping
{
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
int weight, distance, distancex;
double rate, price;
rate = 0.00;
System.out.print("Please enter package weight: ");
weight = input.nextInt();
while (weight <= 0 || weight >= 61)
{
System.out.print("Please enter package weight: ");
}
if (weight <= 10 && weight >= 1 )
{
rate = 5.01;
}
else if ( weight <= 20 && weight >= 11 )
{
rate = 7.02;
}
else if ( weight <= 30 && weight >= 21 )
{
rate = 9.03;
}
else if ( weight <= 40 && weight >= 31 )
{
rate = 11.04;
}
else if ( weight <= 60 && weight >= 41)
{
rate = 15.00;
}
System.out.print("Please enter distance: ");
distance = input.nextInt();
while ( distance <= 0 )
{
System.out.print("Please enter distance: ");
}
distancex = ( distance / 100 ) + 1;
price = ( distancex * rate );
System.out.printf("Your total shipping cost for %d miles is $%.2f\n", distance, price);
}
}
You need to ask the user to enter the weight again inside of the while loop.
while (weight <= 0 || weight >= 61) {
System.out.print("Please enter package weight: ");
weight = input.nextInt();
}
You could also use a do-while loop:
do {
System.out.print("Please enter package weight: ");
weight = input.nextInt();
} while (weight <= 0 || weight >= 61);
If you use the do-while loop, you can remove the first time you ask and the while loop. It's a slightly more compact way of doing it.
I'm trying to create my fist java application with an if statement that will take an integer (e.x 22) and find out if its sum is equal when multiplied and subtract (e.x 2*2=4 and 2+2=4) or if its not.
Though i can't figure out how to do the if decision. can someone point out how to do that?
thank u
package 1;
import java.util.Scanner;
public class 1
{
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
int x;
int y;
System.out.print( "Enter a number from 10 to 99: " );
x = input.nextInt();
if ( x >= 10 && x <= 99 )
{
x= x % 10;
x= x / 10;
}
else
{
System.out.println( "you must enter a number from 10 to 99" );
}
}
}
You just need to assign them to different variable and check for the condition
if (x >= 10 && x <= 99) {
int first = x / 10; // Take out the first digit and assign it to a variable first
int second = x % 10; // Take out the second digit and assign it to a variable second
if (first * second == first + second) { // Check for your condition, which you mentioned in your question
System.out.println("Yep, they match the condition"); // If it satisfies the condition
} else {
System.out.println("Nope, they don't match the condition"); // If it doesn't satisfy the condition
}
}
P.S: You question said multiplied and subtract but the example just after the example was (e.x 2 x 2=4 and 2+2=4). I went with the ex.
try
import java.util.Scanner;
public class One {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x;
int y;
System.out.print("Enter a number from 10 to 99: ");
x = input.nextInt();
if (x >= 10 && x <= 99) {
y = x % 10;
x = x/10 ;
if(x* y== x+ y)){
System.out.println("Sum and product are equal" );
}
else
System.out.println("Sum and product are not equal" );
} else {
System.out.println("you must enter a number from 10 to 99");
}
input.close();
}
}