Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Want to have this code to search for dates between 1950-2050 and find out when Nothing happened, World cup, Olympic games or the info you write does not match any of the dates.
So the OL occurs when the number between 1950-2050 is divided by 4. And the WC occurs between even years of the OL like 2002(2004 WC)2006.
Then we have the dates no OL/WC occur between 1950-2050, that year nothing happened. And last if you put in like 700 it should just say the last else.
Scanner input = new Scanner(System.in);
System.out.println("Write year between 1950-2050: ");
int keyboard = input.nextInt();
int OL = (keyboard);
int WC = (keyboard);
int nothingspec = (keyboard);
int instru = (keyboard);
if(nothingspec) {
System.out.println("This year nothing special happened.");
}
else if(OL) {
System.out.println("Yes this year it the olympic games. ");
}
else if(WC) {
System.out.println("Yes this year it was a world cup in soccer.");
}
else(instru) {
System.out.println("Your instructions were wrong please try again.");
}
input.close();
As much as i understand it should be something like this
System.out.println("Write year between 1950-2050: ");
int keyboard = input.nextInt();
int OL = (keyboard);
int WC = (keyboard);
int nothingspec = (keyboard);
int instru = (keyboard);
boolean blOL = false;
boolean blWC = false;
//this occurs whenever the number can be divided by 4
if(keyboard>=1950&&keyboard<=2050){
if(OL%4==0) {
System.out.println("Yes this year it the olympic games. ");
blOL=true;
}
//This will happen every time the date can be divided to 2 so as you said 2002, 2004, 2006 and so on.
else if(WC%2==0) {
System.out.println("Yes this year it was a world cup in soccer.");
blWC = true;
}
//This is when nothing has happend.
else if(blOL==false && blWC==false) {
System.out.println("This year nothing special happened.");
}
else{
System.out.println("Your instructions were wrong please try again.");
}
}
else{
System.out.println("Your instructions were wrong please try again.");
}
input.close();
Please try this and tell me if this is what you needed.
Related
This question already has answers here:
How do I make my code run in a loop and ask the user "Try again Yes or no?"
(4 answers)
Closed 3 years ago.
I'm new on this java programming. And I need to make this program loop
the Question "Do you want to Try Again?! [Yes/No]" and loop it back to
"Enter any year:" question. How will I do that?
I hope you provided me with more elaborated answer or example to make this program work properly within today..
import java.util.Scanner;
public class my_LeapYear {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter any year: ");
int year = s.nextInt();
boolean loop = true;
while(loop == true)
{
if (((year %4==0)&& (year %100 !=0))||(year %400==0))
{
System.out.println("Year "+year+" is a Leap Year");
break;
}
else if ((year %100 == 0)&&(year %400 == 0))
{
} else {
System.out.println("Year "+year+" is not a Leap Year");
loop = false;
break;
}
Try to use if statement that checks whether yes or no is entered , update the value of loop accordingly see below
System.out.println("Do you want to Try Again")
String input = in.nextLine();
if (input.equals("YES")){
loop = true
}
else if (input.equals("NO")){
loop=false
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
Very new to programming. I know this question will be very easy to answer for experienced programmers. I would like to continue running this program which finds leap years until the user enter "n." The program terminates before being able to enter y/n. Help is very much appreciated... thank you.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String another = "y";
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a year ");
int year = scan.nextInt();
while (another.equals("y")) {
if (year < 1582) {
System.out.println("Not an established Gregorian year.");
} else if (year % 4 != 0 || (year % 100 == 0 && year % 400 != 0)) {
System.out.println("Not a leap year");
} else {
System.out.println("Leap year!");
}
System.out.println();
System.out.print("Test another year (y/n)?");
another = scan.nextLine();
}
}
}
The enter from when the person enters the year number at the beginning is not used up. It is stored in the "buffer" and when the program reaches the point where you ask if they want to do another year, it reads the enter that is in the buffer. To fix this you just need another scan.nextLine(); before the important one.
This question already has answers here:
Java do-while loop isn't working
(3 answers)
Closed 7 years ago.
I working on a project for my course. The program runs fine until it hits my end program I've been messing with it for about 2 hours when I thought I was already finished.
This is the code it's messing up on.
do {
System.out.println("Do you want to end program? (Enter n or y):");
endProgram = Input.next();
if(!endProgram.equals("y") || (!endProgram.equals("n"))){
System.out.println("Do you want to end program? (Enter n or y):");
}
if (endProgram.equalsIgnoreCase("n")){
endProgram = "n";
aui = true;
}
if (endProgram.equalsIgnoreCase("y")){
endProgram = "y";
aui = true;
}
} while(aui = false);
I tried messing with the else if then switched to if. Full code is
public static String endProgram = null;
public static void main(String[] args) {
String MUR = "--------------Monthly Use Report--------------";
int minutesAllowed;
int minutesUsed = 0;
int minutesOver;
double totalOwed;
double monthlyRate = 74.99;
double minOver = 0.20;
double realOwed;
boolean valid = false;
boolean over = false;
boolean aui = false;
Scanner Input = new Scanner(System.in);
System.out.println("Welcome to the Cell Phone Minutes Calculator.");
do {
do {
System.out.println("Please input the amount of minutes you were allowed to use per month.");
System.out.println("Please Enter a value between (200 - 800)");
minutesAllowed = Input.nextInt();
} while (minutesAllowed <= 199 || minutesAllowed >= 801);{
}
do{
try{
System.out.println("How many minutes were used during the previous month?");
minutesUsed = Input.nextInt();
if(minutesUsed <= 1){
System.out.println("--Invalid Input! Please use a positive number.--");
} else {
valid = true;
}
} catch(Exception e){
System.out.println("Invalid Input! Please try again.");
Input.next();
}
}while(!valid);
minutesOver = minutesAllowed - minutesUsed;
if(minutesAllowed >= minutesUsed){
System.out.println("You were not over your minutes for the month!");
} else {
System.out.println("You were over your minutes by "+ Math.abs(minutesOver));
over = true;
}
totalOwed = (Math.abs(minutesOver))*(minOver);
realOwed = totalOwed+monthlyRate;
System.out.println(MUR);
System.out.println("Minutes allowed were "+ minutesAllowed);
System.out.println("Minutes used were "+ minutesUsed);
if(over){
System.out.println("Minutes over were "+ Math.abs(minutesOver));
System.out.println("Total due is $"+ realOwed);
} else {
System.out.println("Total due is $"+ monthlyRate);
}
do {
System.out.println("Do you want to end program? (Enter n or y):");
endProgram = Input.next();
if(!endProgram.equals("y") || (!endProgram.equals("n"))){
System.out.println("Do you want to end program? (Enter n or y):");
}
if (endProgram.equalsIgnoreCase("n")){
endProgram = "n";
aui = true;
}
if (endProgram.equalsIgnoreCase("y")){
endProgram = "y";
aui = true;
}
} while(aui = false);
}while((endProgram.equalsIgnoreCase("n")) && (aui = false));
}
}
Sorry if the code is sloppy. When I run the Program it runs properly unless I put two improper user inputs. such as,
Program running//
--------------Monthly Use Report--------------
Minutes allowed were 450
Minutes used were 500
Minutes over were 50
Total due is $84.99
Do you want to end program? (Enter n or y):
g
Do you want to end program? (Enter n or y):
If I add Input.Next(); to nest if statement to
if(!endProgram.equals("y") || (!endProgram.equals("n"))){
System.out.println("Do you want to end program? (Enter n or y):");
endProgram = Input.next();
it displays it correctly. I tried messing with the massive do while loop that goes across the whole project. If anybody can help me it will be much appreciated. Sorry if this is confused I'll respond if you guys have any questions. Thanks in advance for any response and sorry for the inconveniences.
Replace
while (aui = false); //here you are assigning aui to false value
to
while (aui == false); //here you are comparing aui to false value
= is as assignment operator, == is comparison operator.
The best practise is to use boolean directly, not by comparing:
while (!aui);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
7267881 is in my file as an account and after the file is scanned if you do
System.out.println(Account[2][1]);
it prints back 7267881, but when prompted if the user enters 7267881 it returns that it is an invalid number... all the other account numbers in the file work just not this one... why?
import java.io.File;
import java.util.Scanner;
public class AcccountArray {
public static void main(String[] args)
{
//Scan the file and save account details to array
File file = new File ("customers.txt");
System.out.println("Path : " + file.getAbsolutePath());
try{
Scanner scanner = new Scanner(new File("customers.txt"));
String[][] Account = new String[Integer.valueOf(scanner.nextLine())][3];
for(int i=0;i<Account.length;i++)
{
Account[i][0]=scanner.nextLine();
Account[i][1]=scanner.nextLine();
Account[i][2]=scanner.nextLine();
}
scanner.close();
System.out.println(Account[2][1]);
Scanner userinput = new Scanner(System.in);
System.out.println("Please enter account number: ");
String accountNumber = userinput.next();
int matchindex = 0;
Boolean match = false;
for (int k =0;k<Account.length;k++)
{
if(Account[k][1].equals(accountNumber))
{
match = true;
matchindex = k;
}
}
if(match)
{
Account ac = new Account();
ac.toString(Account[matchindex][0], Account[matchindex][1], Account[matchindex][2]);
System.out.println("Enter 'D' for deposite\nEnter 'W' for withdrawal\nEnter 'Q' for quit");
Scanner transaction = new Scanner(System.in);
String type = transaction.next();
Scanner ammount = new Scanner(System.in);
switch (type) {
case "D":
System.out.println("Enter the ammount : ");
float diposit = ammount.nextFloat();
float curent = Float.valueOf(Account[matchindex][2]);
System.out.println("New balance = "+(curent+diposit));
break;
case "W":
System.out.println("Enter the ammount : ");
float withdrawal = ammount.nextFloat();
float balance = Float.valueOf(Account[matchindex][2]);
System.out.println("New balance = "+(balance-withdrawal));
break;
case "Q":
System.out.println("Exit");
break;
default:
System.out.println("Invalid transaction");
}
}
else
{
System.out.println("Invalid user account number");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
the file looks like
4
John Anderson
4565413
250.00
Louise Carter
2323472
1250.45
Paul Johnson
7267881
942.81
Sarah Wilson
0982377
311.26
The immediate error is arguably in your input file. There appears to be a space after the account number in question. You need to either make the account number in the file exactly as it will be typed during the probe, or trim the strings on input.
However, you do need to learn how to debug effectively. I have some advice on how to do it, with a worked example in Java, at Debug Strategy.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to create a counter in a dr java program
this is the code that I am working on...
I would like to know how to reset the interactions window after every question is asked so that the window is clear for the next question to be shown to the user but I do not want the Score to be reset either so that I can display the score at the end. I am looking for the correct syntax and code to do this.
//Neel Patel
//Friday October 9th, 2009
/*This is a quiz program that will ask the user 10 questions. the user will answer
* these questions and will be scored out of 10.*/
class Quiz
{
public static void main (String args[])
{
//Instructions
System.out.println("instructions");
System.out.println(" ");
System.out.println("1. You wll be asked ten questions through out the quiz.");
System.out.println("2. The first question will appear, you will have to answer that question for the next question to appear.");
System.out.println("3. When you answer the last question you will be told your score.");
System.out.println(" ");
System.out.println("welcome to the basketball quiz.");
int Score=0;
// question 1
System.out.println(" ");
System.out.println("Question 1. ");
System.out.println("How tall is a basketball hoop? ");
System.out.println("Type in Answer here:");
String Question1= In.getString();
if (Question1.equalsIgnoreCase("10 Feet"))
{
Score++;
System.out.println("Correct!");
}
else
{
System.out.println("you got this questions wrong");
}
// question 2
System.out.println(" ");
System.out.println("Question 2. ");
System.out.println("Who invented basketball? ");
System.out.println("Type in Answer here:");
String Question2= In.getString();
if (Question2.equalsIgnoreCase("James Naismith"))
{
Score++;
System.out.println("Correct!");
}
else
{
System.out.println("you got this questions wrong");
}
// question 3
System.out.println(" ");
System.out.println("Question 3. ");
System.out.println("Who is the only person in the history of the NBA to average a triple double for an entier season?");
System.out.println("Type in Answer here:");
String Question3= In.getString();
if (Question3.equalsIgnoreCase("Oscar Robertson"))
{
Score++;
System.out.println("Correct!");
}
else
{
System.out.println("you got this questions wrong");
}
// question 4
System.out.println(" ");
System.out.println("Question 4. ");
System.out.println("how many players was the first basketball game played with?");
System.out.println("Type in Answer here:");
String Question4= In.getString();
if (Question4.equalsIgnoreCase("9 on 9||18"))
{
Score++;
System.out.println("Correct!");
}
else
{
System.out.println("you got this questions wrong");
}
}
}
You need to use a loop of some sort. So you can start by creating arrays to store the questions and answers like this:
String[] questions = {" \nQuestion 1. \nHow tall is a basketball hoop? \nType in Answer here:",
" \nQuestion 2. \nWho invented basketball? \nType in Answer here: "};
String[] answers = {"10 Feet", "James Naismith"};
int score = 0;
String ans = "";
Then you can write a loop like this:
for(int i = 0;i < questions.length; i++){
System.out.println(questions[i]);
ans= In.getString();
if (ans.equalsIgnoreCase(answers[i]))
{
System.out.println("Correct!");
score++;
}
else
{
System.out.println("you got this questions wrong");
}
}
System.out.println(score);
And finally to clear the screen itself you cannot do that directly in Java but you may be able to run the cls command (assuming you run Windows) but that makes your code platform specific.
Runtime.getRuntime().exec("cls");
One brute-force (and, unlike cls, platform-independent) method for clearing the console would be to print a whole bunch of newliness to the console. This approach could have the added benefit of being seen as befitting an 11th grade CS project.