Created
March 6, 2019 01:57
-
-
Save jaohaohsuan/3ae3b3383a1e911d12c856c8850ff514 to your computer and use it in GitHub Desktop.
week 3 class practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comp608.assignment1; | |
import static java.lang.System.out; | |
import java.util.*; | |
public class Main { | |
private static final Scanner input = new Scanner(System.in); | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
String s1 = input.nextLine(); | |
out.println(s1 == "h"); | |
//validateNumberInRange(); | |
//validateDiscreteStringValues(); | |
readMultipleInputWithLoop(); | |
} | |
public static void readMultipleInputWithLoop() { | |
int size = 0; | |
while(size < 1) { | |
out.print("How many numbers to enter? "); | |
size = input.nextInt(); | |
if(size < 1) { | |
out.println("Invalid. Try agian."); | |
} | |
} | |
double total = 0; | |
double val; | |
int counter = 1; | |
while(size > 0) { | |
out.printf("Enter value #%d: ", counter++); | |
val = input.nextDouble(); | |
total = total + val; | |
size--; | |
} | |
out.printf("Total is %-10.2f\n", total); | |
} | |
public static void validateDiscreteStringValues( ) { | |
String val; | |
while(true) { | |
out.print("Are you hungry(yes/no)? "); | |
val = input.nextLine(); | |
if(val.equalsIgnoreCase("yes")) { | |
actionsYes(); | |
break; | |
} else if(val.equalsIgnoreCase("no")) { | |
actionsNo(); | |
break; | |
} else { | |
out.printf("Invalid: %s, enter yes/no.\n", val); | |
} | |
} | |
} | |
public static void actionsNo() { | |
out.println("Actions to take for no."); | |
} | |
public static void actionsYes() { | |
out.println("Actions to take for yes."); | |
} | |
public static void validateNumberInRange() { | |
double num; // store user input | |
while(true) { | |
out.print("Enter a number(10-20 incl.): "); | |
num = input.nextDouble(); | |
if(num >= 10 && num <= 20 ) | |
break; | |
else | |
out.printf("Input %f is invalid.\n", num); | |
} | |
out.printf("Make use of user input here: %.0f\n", num); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment