I need to develop a scholarship application in Java. I need to print the successful application in output.
Why a counter in this code does not increase if I for a second person?
import java.util.Scanner;
import java.util.LinkedList;
public class Main
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
Application a = new Application();
LinkedList lList = new LinkedList();
boolean bEligible = false;
int scholarship;
int scholarshipAmount = 500000;
int setiStudyLevel;
int counter;
while(scholarshipAmount != 0)
{
counter = 1;
System.out.println("");
System.out.println("**Welcome to scholarship applications offered by Smart Foundation**");
System.out.println("");
System.out.println("");
System.out.println("Please Enter Your Name");
a.setsName(input.next());
System.out.println("1–Pre Diploma, 2–Diploma, 3–Degree");
System.out.println("Please Enter Your Study Level");
a.setiStudyLevel(input.nextInt());
System.out.println("Please Enter Your Personality Score");
a.setiPersonalityScore(input.nextInt());
System.out.println("Please Enter Your Parents Income");
a.setdParentsIncome(input.nextDouble());
String sName = a.getsName();
int iStudyLevel = a.getiStudyLevel();
int iPersonalityScore = a.getiPersonalityScore();
double dParentsIncome = a.getdParentsIncome();
if (iStudyLevel == 1)
{
scholarship = 15000;
scholarshipAmount = scholarshipAmount - scholarship;
}
else if (iStudyLevel == 2)
{
scholarship = 50000;
scholarshipAmount = scholarshipAmount - scholarship;
}
else if (iStudyLevel == 3)
{
scholarship = 500000;
scholarshipAmount = scholarshipAmount - scholarship;
}
System.out.println("");
System.out.println("");
System.out.println("*****Result*****");
System.out.println("Name:"+sName);
System.out.println("1–Pre Diploma, 2–Diploma, 3–Degree");
System.out.println("Study Level:"+iStudyLevel);
System.out.println("Personality Score:"+iPersonalityScore);
System.out.println("Parents Income:"+dParentsIncome);
if(iPersonalityScore <=90)
{
if(dParentsIncome >=3000)
{
System.out.println("YOU ARE NOT ELIGIBLE FOR SCHOLARSHIP ");
}
else
{
System.out.println("YOU ARE ELIGIBLE FOR SCHOLARSHIP ");
bEligible = true;
}
}
else
{
System.out.println("YOU ARE ELIGIBLE FOR SCHOLARSHIP ");
}
System.out.println("");
System.out.println( "RM"+(scholarshipAmount)+" left!!");
System.out.println("");
if (scholarshipAmount <= 0)
{
System.out.println(" Scholarship Quota is Full!");
}
counter++;
System.out.println(counter);
}
}
}
The problem is that you reset your counter to 1 on each loop
Change this:
while(scholarshipAmount != 0)
{
counter = 1;
to this
int counter = 1;//or maybe 0
while(scholarshipAmount != 0)
{
Your loop should be,
while(scholarshipAmount > 0)
{
//remaining code
Related
So I'm trying to make this car racing game, modeled after horse races. I've tested the game without the betting component and it works the way I want it to, so thats no problem. But once I started to implement the betting, the program kinda gave up on me. After the user is prompted to enter the bet, the program is just blank.
I'm not getting any error messages and I've tried debugging, but I just really can't figure it out.
I've attached the whole code minus the header; all it is is the names of the cars. (1,2,3)
public static void main(String[]args)throws IOException, InterruptedException{ //start main
Scanner in;
in = new Scanner(System.in);
boolean doAnother = true;
printHeader();
printGame();
while (doAnother) { //start while
int response;
System.out.print("\nWanna play again? 1 = yes, 2 = no.");
response = in.nextInt();
if (response == 1) { // start if loop
doAnother = true;
printHeader();
printGame();
} // end if loop
else {
doAnother = false;
System.out.println("Come back again soon!");
}
} //end while look
} //end main
public static boolean printGame()throws IOException, InterruptedException{
Scanner in = new Scanner(System.in);
Random rd = new Random();
int car = in.nextInt();
if (car >= 4) {
System.out.println("THATS NOT AN OPTION! You automatically lose.");
} else
bet();
System.out.println("Commence race!");
Thread.sleep(250);
int lambo = 0,
nissan = 0,
egg = 0;
int track = 100;
while (true) {
for (int i = 0; i < 50; i++)
System.out.println();
for (int i = 0; i < track; i++) {
System.out.print("-");
}
System.out.println();
lambo = lambo + rd.nextInt(4) + rd.nextInt(2) - rd.nextInt(2);
nissan = nissan + rd.nextInt(4) + rd.nextInt(2) - rd.nextInt(2);
egg = egg + rd.nextInt(4) + rd.nextInt(2) - rd.nextInt(2);
for (int i = 0; i < lambo; i++) //L
{
System.out.print("."); //distance travelled
}
System.out.println("ℾ");
//
for (int i = 0; i < nissan; i++) //M
{
System.out.print("."); //distance travelled
}
System.out.println("ℿ");
//
for (int i = 0; i < egg; i++) //T
{
System.out.print("."); //distance travelled
}
System.out.println("⅀");
//
for (int i = 0; i < track; i++) {
System.out.print("-");
}
System.out.println();
//
Thread.sleep(250);
//
if (nissan > track || lambo > track || egg > track) {
break;
}
}
if (car == track) {
System.out.println("\nYour car won! Rad!");
return true;
} else {
System.out.println("\nYour car lost... bummer.");
return false;
}
} //printGame
public static void bet()throws IOException, InterruptedException{
Scanner in;
in = new Scanner(System.in);
int money;
int bet;
boolean userWins = true;
money = 100;
while (true) {
System.out.println("You have " + money + " dollars.");
do {
System.out.println("How much do you wanna bet? Or, enter 0 to walk away.)");
System.out.print("$");
bet = in.nextInt();
if (bet < 0 || bet > money) {
System.out.println("Your bet must be between 0 and " + money + '.');
}
} while (bet < 0 || bet > money);{
if (bet == 0) {
System.out.println("Bye.");
break; //walk away
} else {
userWins = printGame();
}
if (userWins == true) {
money = money + bet;
}
if (userWins == false) {
money = money - bet;
System.out.println();
}
if (money == 0) {
System.out.println("Aw shoot, looks like you've are out of money!");
break;
}
}
}
System.out.println();
System.out.println("You walk away with $" + money + '.');
} //end method
You can't have two scanners nested like that. You must use the same scanner from the main method in you other methods or close that scanner
I am a new human learning to code!
I had a problem with my Scanner, which is that I need it to 'reset' on an invalid character.
My code:
public class Lemonade {
static int m = 150;
private static Scanner scan;
public static void main(String[] args) {
int day = 1;
for(int gameover = m; gameover > 0; day++) {
int Random = (int) (Math.random() * 100);
if(Random <= 25) {
System.out.println("Great Chance!");
System.out.println("--------------------------------");
}
else if(Random <= 50) {
System.out.println("Good Chance!");
System.out.println("--------------------------------");
}
else if(Random <= 75) {
System.out.println("Bad Chance!");
System.out.println("--------------------------------");
}
else if(Random <= 100) {
System.out.println("Awful Chance!");
System.out.println("--------------------------------");
}
int count = 0;
int none = 0;
scan = new Scanner(System.in);
System.out.println("Enter a number between 0 and " + m + "!");
count = scan.nextInt();
if(count >= none && count <= m) {
System.out.println("You entered " + count + "!");
System.out.println("--------------------------------");
day = day + 1;
m = m - count;
System.out.println("Day " + day);
}
else {
System.out.println("Enter a number between 0 and " + m + ".");
count = scan.nextInt();
}
}
}
}
Now is my question how to get this to 'reset' on an invalid character like 'f', as Scanner only accepts numbers.
Thanks for the help!
If I understand you correctly then this is something you're looking for,
InputMismatchException will thrown if user enters invaild characters instead of int. You may use looping until the user enters an integer
import java.util.Scanner;
import java.util.InputMismatchException;
class Example
{
public static void main(String args[])
{
boolean isProcessed = false;
Scanner input = new Scanner(System.in);
int value = 0;
while(!isProcessed)
{
try
{
value = input.nextInt();
//example we will now check for the range 0 - 150
if(value < 0 || value > 150) {
System.out.println("The value entered is either greater than 150 or may be lesser than 0");
}
else isProcessed = true; // If everything is ok, Then stop the loop
}
catch(InputMismatchException e)
{
System.out.print(e);
input.next();
}
}
}
}
If this is not you're looking for please let me know!
I am working on a Java project and am encountering a strange error. I have a menu with multiple options all inside a do while loop but I want the variable pizzasOrdered to keep increasing when the user adds more pizzas to his order. Here is my code:
import java.util.Scanner;
public class PizzaMenu {
public static void main(String[] args) {
int numPlain;
int numPepperoni;
int pizzasOrdered = 0;
boolean flag = true;
System.out.println("Welcome to Pies, Pies, and pis!");
Scanner kbd = new Scanner(System.in);
System.out.println("Is there a customer in line? (1 = yes, 2 = no)");
int isCustomer = kbd.nextInt();
System.out.println("Are you a Pie Card member? (1 = yes, 2 = no)");
boolean isMember;
int pieCardResponse = kbd.nextInt();
if(pieCardResponse == 1)
{
isMember = true;
}
else
{
isMember = false;
}
do{
System.out.println("Please choose an option: \n\t1) Update Pizza Order\n\t2) Update Cherry Pie Order\n\t3) Update Charm Order\n\t4) Check Out");
int option = kbd.nextInt();
if(option == 1)
{
System.out.println("Here is your current order: \n\t" + pizzasOrdered + " pizzas ordered");
System.out.println("How many plain pizzas would you like for $10.00 each?");
numPlain = kbd.nextInt();
System.out.println("How many pepperoni pizzas would you like for $12.00 each?");
numPepperoni = kbd.nextInt();
pizzasOrdered = numPlain + numPepperoni;
}
else if(option == 2)
{
System.out.println("Here is your current order: \n");
}
else if(option == 3)
{
}
else if(option == 4)
{
}
}while(flag);
}
}
You should change this
pizzasOrdered = numPlain + numPepperoni;
to
pizzasOrdered += (numPlain + numPepperoni);
In your case you are not incrementing the variable, you are just assigning it new values in every iteration.
This code models a simple ATM machine that can deposit, withdraw, and show the 10 latest transactions in an array of 10 index values.
My problem is that I can't get the balance right after 10 deposits, the balance only seems to sum the values in my array, not including the transactions made before those ten. What am I doing wrong?
import java.util.Scanner;
public class cashier2 {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int amount = 0;
int choice = 0;
int[] transactions = new int[10];
int sum = 0;
int balance = 0;
while(choice != 4)
{
choice = menu();
switch(choice)
{
case 1:
System.out.print("deposit");
sum = keyboard.nextInt();
if(sum == 0)
{
System.out.print("wrong amount");
System.out.println();
System.out.println();
}
else
{
amount = (int) + sum;
makeTransactions(amount, transactions);
}
break;
case 2:
System.out.print("withdrawal ");
sum = keyboard.nextInt();
if(sum == 0)
{
System.out.print("wrong amount");
System.out.println();
System.out.println();
}
else
{
amount = (int) - sum;
makeTransactions(amount, transactions);
}
break;
case 3:
showTransactions(transactions, balance);
break;
case 4:
System.out.println("Exit");
break;
}
}
}
public static int menu()
{
Scanner keyboard = new Scanner(System.in);
int choice = 0;
System.out.println("Simple atm");
System.out.println();
System.out.println("1. deposit");
System.out.println("2. withdrawal");
System.out.println("3. balance");
System.out.println("4. exit");
System.out.println();
System.out.print("your choice: ");
choice = keyboard.nextInt();
return choice;
}
public static void showTransactions(int[] transactions, int balance)
{
System.out.println();
System.out.println("Tidigare transaktioner: ");
System.out.println();
for(int i = 0; i < transactions.length; i++)
{
if(transactions[i] == 0)
{
System.out.print("");
}
else
{
System.out.print(transactions[i] + "\n");
balance = balance + transactions[i];
}
}
System.out.println();
System.out.println("Saldo: " + balance + " kr" + "\n");
System.out.println();
}
public static void makeTransactions(int amount, int[] transactions)
{
int position = findNr(transactions);
if (position == -1)
{
moveTrans(transactions);
position = findNr(transactions);
transactions[position] = amount;
}
else
{
transactions[position] = amount;
}
}
public static int findNr(int[] transactions)
{
int position = -1;
for(int i = 0; i < transactions.length; i++)
{
if (transactions[i] == 0)
{
position = i;
break;
}
}
return position;
}
//}
public static void moveTrans(int [] transactions)
{
for(int i = 0; i < transactions.length-1; i++)
{
transactions[i] = transactions[i + 1];
}
transactions[transactions.length-1] = 0;
}
}
What I really am stuck with is the second to last variable userGuessDifference. It remains at zero making my second while loop not run properly as it just keeps going back to the first else if statement.
public class GuessingGame {
/**
* #param args
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
Random generator = new Random();
int difficulty = 0;
int guesses = 0;
int userGuess = 0;
int correctAnswer = 0;
int counter = 0;
int userGuessDifference = (Math.abs(correctAnswer) - Math.abs(userGuess));
boolean flag = false;
System.out.println("We are going to play a number guessing game.");
System.out.println(" ");
System.out.println("Choose your difficulty:");
System.out.println("Pick a number - 10 is easy, 25 is medium, 50 is hard.");
difficulty = keyboard.nextInt();
if (difficulty == 10)
{
guesses = 3;
System.out.println("You have 3 guesses, make them count!");
}
else if (difficulty == 25)
{
guesses = 5;
System.out.println("You have 5 guesses, make them count!");
}
else if (difficulty == 50)
{
guesses = 6;
System.out.println("You have 6 guesses, make them count!");
}
else
{
System.out.println("If you can't follow instructions, I'm going to make this very difficult for you!");
difficulty = (difficulty * 100);
guesses = 1;
}
System.out.println(" ");
System.out.println("Ok, I have my number. Time to play.");
correctAnswer = generator.nextInt(difficulty) + 1;
System.out.println("Pick a whole number between 1 and " + difficulty + ":");
userGuess = keyboard.nextInt();
while (!flag || (counter <= guesses))
{
if (userGuess == correctAnswer)
{
System.out.println("CONGRATS YOU WIN!");
flag = true;
}
else if ((userGuessDifference <= (difficulty * .10)))
{
System.out.println("HOT!");
userGuess = keyboard.nextInt();
counter++;
}
else if ((userGuessDifference < (difficulty * .25)) && (userGuessDifference > (difficulty * .10)))
{
System.out.println("Warm...");
userGuess = keyboard.nextInt();
counter++;
}
else
{
System.out.println("Ice cold.");
userGuess = keyboard.nextInt();
counter++;
}
}
}
}
As #SotiriosDelimanolis wrote, you never reassign userGuessDifference. This should be done inside the while loop.
Moreover, there is another problem with your code: if you guess the number, the program just prints "CONGRATS YOU WIN!" forever, but it seems to me that you wanted to quit from the while loop once the user guesses the number (I guess the flag variable was introduced for this reason).
I slightly changed your code in order to meet this requirement:
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random generator = new Random();
int difficulty = 0;
int guesses = 0;
int userGuess = 0;
int correctAnswer = 0;
int counter = 0;
System.out.println("We are going to play a number guessing game.");
System.out.println(" ");
System.out.println("Choose your difficulty:");
System.out.println("Pick a number - 10 is easy, 25 is medium, 50 is hard.");
difficulty = keyboard.nextInt();
if (difficulty == 10) {
guesses = 3;
System.out.println("You have 3 guesses, make them count!");
} else if (difficulty == 25) {
guesses = 5;
System.out.println("You have 5 guesses, make them count!");
} else if (difficulty == 50) {
guesses = 6;
System.out.println("You have 6 guesses, make them count!");
} else {
System.out.println("If you can't follow instructions, I'm going to make this very difficult for you!");
difficulty = (difficulty * 100);
guesses = 1;
}
System.out.println(" ");
System.out.println("Ok, I have my number. Time to play.");
correctAnswer = generator.nextInt(difficulty) + 1;
System.out.println("Pick a whole number between 1 and " + difficulty + ":");
userGuess = keyboard.nextInt();
while (counter <= guesses) {
// int userGuessDifference = (Math.abs(correctAnswer) - Math
// .abs(userGuess));
int userGuessDifference = Math.abs(correctAnswer - userGuess);
if (userGuess == correctAnswer) {
System.out.println("CONGRATS YOU WIN!");
break;
}
else if ((userGuessDifference <= (difficulty * .10))) {
System.out.println("HOT!");
}
else if ((userGuessDifference < (difficulty * .25))
&& (userGuessDifference > (difficulty * .10))) {
System.out.println("Warm...");
}
else {
System.out.println("Ice cold.");
}
userGuess = keyboard.nextInt();
counter++;
}
}
}