PhoenixTears
|
|
« Reply #6 on: August 02, 2008, 02:21:25 pm » |
|
Kinda odd... but oh well. This would be a calculator in Java. import java.util.*; public class calculator { public static void main(String args[]) { Scanner input=new Scanner(System.in); char op = 'i'; System.out.println("Welcome to calculator. If you would like to quit calculator, press 'q'.\n" + "To add, press '+'. To subtract, press '-'. To multiply, press '*'.\n" + "To divide, press '/'. To apply modulus, press '%'."); while (op!='q') { System.out.print("Choose an operation (+, -, *, /, %) : "); op = input.nextLine().charAt(0); double a, b, c = 0; if (op =='+') { System.out.print("This function will add two numbers.\nEnter the first number: "); a=input.nextInt(); System.out.print("Enter the second number: "); b=input.nextInt(); c=a+b; System.out.print("The sum is "); System.out.print(c); System.out.print(".\n"); } else if (op =='-') { System.out.print("This function will subtract two numbers.\nEnter the first number: "); a=input.nextInt(); System.out.print("Enter the second number: "); b=input.nextInt(); c=a-b; System.out.print("The difference is "); System.out.print(c); System.out.print(".\n"); } else if (op =='*') { System.out.print("This function will multiply two numbers.\nEnter the first number: "); a=input.nextInt(); System.out.print("Enter the second number: "); b=input.nextInt(); c=a*b; System.out.print("The product is "); System.out.print(c); System.out.print(".\n"); } else if (op =='/') { System.out.print("This function will divide two numbers.\nEnter the dividend: "); a=input.nextInt(); System.out.print("Enter the divisor: "); b=input.nextInt(); c=a/b; System.out.print("The quotient is "); System.out.print(c); System.out.print(".\n"); } else if (op =='%') { int x, y, z = 0; System.out.print("This function will give the remainder after dividing two integers." + "\nEnter the dividend: "); x=input.nextInt(); System.out.print("Enter the divisor: "); y=input.nextInt(); z=x%y; System.out.print("The remainder is "); System.out.print(z); System.out.print(".\n"); } input.nextLine(); } } } Uh, prime number test. import java.util.Scanner; public class primenumbertest { public static void main (String args[]) { Scanner input=new Scanner(System.in); System.out.println("You can use this program to check if a number is a prime number.\n" + "Negative numbers will not work. Press 'q' to exit."); char op='i'; while (op!='q') { boolean prime=true; System.out.print("Enter a number: "); int number=input.nextInt(); if (number<0) { System.out.print("This number is not a valid number.\n"); prime=false; input.nextLine(); } for (int i=2;number>i;i++) { if (number%i == 0) { System.out.print("This number is not prime.\n"); prime=false; input.nextLine(); break; } else { continue; } } if (prime) { System.out.print("This number is prime.\n"); } } } } A different prime number test import java.util.*; public class primenumbertest2 { public static void main (String args[]) { Scanner input=new Scanner(System.in); System.out.print("This program will display all prime numbers below your " + "inputted integer. Negative numbers are not valid.\nPress 'q' to quit.\n"); char quit='i'; while (quit!='q') { System.out.print("Enter a number: "); int number=input.nextInt(); if (number<3 && number>=0) { System.out.print("There are no prime numbers less than this number.\n"); } else if (number <0) { System.out.print("This number is negative. It is not a valid number.\n"); } else { System.out.print("2"); for (int i=3;number>i;i++) { for (int n=2;n<i;n++) { if (i%n == 0) { break; } else if (n == i-1) { System.out.print(", "); System.out.print(i); break; } } } System.out.print("\n"); } } } } Grade book sort of thing. import java.util.*; /*This is actually a grade book. =3*/ public class triplearray { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("Please enter how many students you have: "); int numstudents=input.nextInt(); System.out.print("Please enter how many grades each student will have: "); int numgrades=input.nextInt(); input.nextLine(); String students[]=new String [numstudents]; int gradebook [][]=new int [numstudents][numgrades]; char choice='o'; while (choice!='q' && choice!='Q') { System.out.print("What would you like to do?\nA. Enter student names.\n" + "B. Enter students grades.\nC. View student names.\n" + "D. View student grades.\nE. Find Student Averages\nQ. Exit gradebook.\n"); choice=input.nextLine().charAt(0); if (choice=='a' || choice=='A') { for (int i=0;i<numstudents;i++) { System.out.print("Please input the name for student "); System.out.print(i+1); System.out.print(": "); students[i]=input.nextLine(); } } else if (choice=='b' || choice=='B') { char yn='o'; while(yn!='n') { System.out.print("Which student's grades would you like to input?\n"); for (int i=0;i<numstudents;i++) { System.out.print(i+1); System.out.print(". "); System.out.print(students[i]); System.out.print("\n"); } int stud=input.nextInt()-1; while(stud<0 || stud>=numstudents) { System.out.print("Sorry, but that is an invalid student number. Please choose again.\n"); stud=input.nextInt()-1; } System.out.print("Please enter "); System.out.print(students[stud]); System.out.print("'s grades.\n"); for (int o=0;o<numgrades;o++) { System.out.print("Please enter the the grade for test "); System.out.print(o+1); System.out.print(": \n"); gradebook[stud][o]=input.nextInt(); input.nextLine(); } System.out.print("Would you like to input anyone else's grades? y/n "); yn=input.nextLine().charAt(0); } } else if (choice=='c' || choice=='C') { System.out.print("Here are the student names.\n"); for (int i=0;i<numstudents;i++) { System.out.print(students[i]); System.out.print("\n"); } } else if (choice=='d' || choice=='D') { System.out.print("Please choose a students name to view their grades.\n"); char yn='o'; while (yn!='n') { for (int i=0;i<numstudents;i++) { System.out.print(i+1); System.out.print(". "); System.out.print(students[i]); System.out.print("\n"); } System.out.print(numstudents+1); System.out.print(". Or choose this option to view all students' grades.\n"); int stud=input.nextInt()-1; while(stud<0 || stud>numstudents) { System.out.print("Sorry, but that is an invalid choice. Please choose again."); stud=input.nextInt()-1; } input.nextLine(); if (stud>-1 && stud <numstudents) { System.out.print("These are "); System.out.print(students[stud]); System.out.print("'s grades.\n"); for (int o=0;o<numgrades;o++) { System.out.print("Test "); System.out.print(o+1); System.out.print(": "); System.out.println(gradebook[stud][o]); } } else if (stud==numstudents) { System.out.print("These are everyone's grades.\n"); for(int q=0;q<numstudents; q++) { System.out.println(students[q]); for(int w=0;w<numgrades;w++) { System.out.print("Test "); System.out.print(w+1); System.out.print(": "); System.out.println(gradebook[q][w]); } } } System.out.print("Would you like to view another student's grades? y/n"); yn=input.nextLine().charAt(0); } } else if(choice=='e' || choice=='E') { char yn='o'; while (yn!='n') { System.out.print("Please choose a students name to view their averages.\n"); for (int i=0;i<numstudents;i++) { System.out.print(i+1); System.out.print(". "); System.out.print(students[i]); System.out.print("\n"); } System.out.print(numstudents+1); System.out.print(". Or choose this option to view all students' averages and the class average.\n"); int stud=input.nextInt()-1; while(stud<0 || stud>numstudents) { System.out.print("Sorry, but that is an invalid choice. Please choose again."); stud=input.nextInt()-1; } input.nextLine(); if (stud>-1 && stud <numstudents) { int accumulator=0; System.out.print(students[stud]); System.out.print("'s average is "); for (int i=0;i<numgrades;i++) { int value=0; value=gradebook[stud][i]; accumulator+=value; } System.out.println(accumulator/numgrades); } else if (stud==numstudents) { System.out.print("These are everyone's averages and the class average.\n"); int accum=0; int val=0; for(int q=0;q<numstudents; q++) { System.out.print(students[q]); System.out.print(": "); int value=0; int accumulator=0; for(int w=0;w<numgrades;w++) { value=gradebook[q][w]; accumulator+=value; } System.out.println(accumulator/numgrades); val=accumulator/numgrades; accum+=val; } System.out.print("The class average is: "); System.out.println(accum/numstudents); } System.out.print("Would you like to view another student's averages? y/n"); yn=input.nextLine().charAt(0); } } } } } Game. Guessing random numbers. import java.util.*; public class randomnumbergame { public static void main(String args[]) { Scanner input=new Scanner(System.in); Random XD=new Random(); System.out.print("Welcome to the Random Number Game!" + "Try to guess the correct number between 1 and 100. You have ten guesses.\n"); int guess=0; char choice='e'; while (choice!='n') { int number=XD.nextInt(100)+1; boolean decide=false; for (int i=1;i<11;i++) { System.out.print("Enter a guess: "); guess=input.nextInt(); input.nextLine(); if (guess == number) { decide=true; System.out.print("You have guessed correctly! The answer is "); System.out.print(number); System.out.println("."); break; } else if (guess < number) { System.out.print("You guessed too low! Try again!\nYou have "); System.out.print(10-i); System.out.print(" tries left.\n"); } else if (guess > number) { System.out.print("You guessed too high! Try again!\nYou have "); System.out.print(10-i); System.out.print(" tries left.\n"); } } if (decide==false) { System.out.print("Sorry, you ran out of guesses! The correct answer was "); System.out.print(number); System.out.println("."); } System.out.print("Would you like to play again? y/n "); choice=input.nextLine().charAt(0); } System.out.print("Thank you for playing!"); } } Final Project game! Made it error proof. import java.util.*; public class Jumpinggame { public static void main(String [] args) { Scanner input=new Scanner(System.in); System.out.println("Welcome to the Jumping Game! "); System.out.print("Would you like to read the instructions? y/n "); String choice=input.nextLine(); while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("yes") && !choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("no")) { System.out.print("Sorry, that is an invalid input. "); choice = input.nextLine(); } if(choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("yes")) { System.out.print("The objective of this game is to get as many pegs as possible off the board.\n" + "If you have one peg left, you are the ultimate winner!!!\n" + "The board will start with one empty space. The player will be able to place this space " + "anywhere they want.\nEvery other space will be filled with pegs. The X-axis will be " + "numbered from 1-9, and the Y-axis marked with letters from A-I.\n" + "To remove pegs, 'jump' one of the pegs over another into an empty space. You can only move " + "left, right, up, and down.\nThe peg that was jumped over is removed from the board. Continue " + "playing until there are no moves left.\n' ' represent an empty space. '/' represents a space that " + "is not avaible for moving.\b represents a peg.\n"); } System.out.print("Would you like to play? y/n "); String play=input.nextLine(); while (!play.equalsIgnoreCase("n") && !play.equalsIgnoreCase("no") && !play.equalsIgnoreCase("y") && !play.equalsIgnoreCase("yes")) { System.out.println("Sorry, that is an invalid input."); play = input.nextLine(); } boolean played=true; while (!play.equalsIgnoreCase("n") && !play.equalsIgnoreCase("no")) { played=false; char gameboard[][]= { {'/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/'}, {'/', '/', ' ', ' ', '1', ' ', '2', ' ', '3', ' ', '4', ' ', '5', ' ', '6', ' ', '7', ' ', '8', ' ', '9', '/', '/', '/', '/'}, {'/', '/', 'A', ' ', '/', '/', '/', '/', '/', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '/', '/', '/', '/', '/', '/', '/', '/', '/'}, {'/', '/', 'B', ' ', '/', '/', '/', '/', '/', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '/', '/', '/', '/', '/', '/', '/', '/', '/'}, {'/', '/', 'C', ' ', '/', '/', '/', '/', '/', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '/', '/', '/', '/', '/', '/', '/', '/', '/'}, {'/', '/', 'D', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', '/', '/', '/', '/'}, {'/', '/', 'E', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', '/', '/', '/', '/'}, {'/', '/', 'F', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '\b', '/', '/', '/', '/'}, {'/', '/', 'G', ' ', '/', '/', '/', '/', '/', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '/', '/', '/', '/', '/', '/', '/', '/', '/'}, {'/', '/', 'H', ' ', '/', '/', '/', '/', '/', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '/', '/', '/', '/', '/', '/', '/', '/', '/'}, {'/', '/', 'I', ' ', '/', '/', '/', '/', '/', ' ', '\b', ' ', '\b', ' ', '\b', ' ', '/', '/', '/', '/', '/', '/', '/', '/', '/'}, {'/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/'}, {'/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/', '/'} }; int row=0; int column=0; for (int i = 1; i < 11; i++) { for (int j = 2; j < 21; j++) { System.out.print(gameboard[i][j]); } System.out.println(); } while(column<4||(column<10 && row<5)||(column<10 && row>7)||(column>14 && row<5)||(column>14 && row>7)||column>20||row<2||row>10) { System.out.print("Please choose a valid beginning empty space: "); String choosing=input.nextLine(); if(choosing.length()!=2) choosing="A1"; if(choosing.charAt(0)=='A' || choosing.charAt(0)=='a') row=2; else if(choosing.charAt(0)=='B' || choosing.charAt(0)=='b') row=3; else if(choosing.charAt(0)=='C' || choosing.charAt(0)=='c') row=4; else if(choosing.charAt(0)=='D' || choosing.charAt(0)=='d') row=5; else if(choosing.charAt(0)=='E' || choosing.charAt(0)=='e') row=6; else if(choosing.charAt(0)=='F' || choosing.charAt(0)=='f') row=7; else if(choosing.charAt(0)=='G' || choosing.charAt(0)=='g') row=8; else if(choosing.charAt(0)=='H' || choosing.charAt(0)=='h') row=9; else if(choosing.charAt(0)=='I' || choosing.charAt(0)=='i') row=10; if(choosing.charAt(1)=='1') column=4; else if(choosing.charAt(1)=='2') column=6; else if(choosing.charAt(1)=='3') column=8; else if(choosing.charAt(1)=='4') column=10; else if(choosing.charAt(1)=='5') column=12; else if(choosing.charAt(1)=='6') column=14; else if(choosing.charAt(1)=='7') column=16; else if(choosing.charAt(1)=='8') column=18; else if(choosing.charAt(1)=='9') column=20; } gameboard[row][column]=' '; for (int pegs=44;pegs>1; pegs--) { for (int i = 1; i < 11; i++) { for (int j = 2; j < 21; j++) { System.out.print(gameboard[i][j]); } System.out.println(); } int row1=0; int column1=0; int row2=0; int column2=0; while((column1<4)||(column1<10&&row1<5)||(column1<10&&row1>7)||(column1>14&&row1<5)||(column1>14&&row1>7)||column>20||row<2||row>10) { System.out.print("Please choose a valid peg to move: "); String choosing=input.nextLine(); if(choosing.length()!=2) { choosing="A1"; } if(choosing.charAt(0)=='A' || choosing.charAt(0)=='a') row1=2; else if(choosing.charAt(0)=='B' || choosing.charAt(0)=='b') row1=3; else if(choosing.charAt(0)=='C' || choosing.charAt(0)=='c') row1=4; else if(choosing.charAt(0)=='D' || choosing.charAt(0)=='d') row1=5; else if(choosing.charAt(0)=='E' || choosing.charAt(0)=='e') row1=6; else if(choosing.charAt(0)=='F' || choosing.charAt(0)=='f') row1=7; else if(choosing.charAt(0)=='G' || choosing.charAt(0)=='g') row1=8; else if(choosing.charAt(0)=='H' || choosing.charAt(0)=='h') row1=9; else if(choosing.charAt(0)=='I' || choosing.charAt(0)=='i') row1=10; if(choosing.charAt(1)=='1') column1=4; else if(choosing.charAt(1)=='2') column1=6; else if(choosing.charAt(1)=='3') column1=8; else if(choosing.charAt(1)=='4') column1=10; else if(choosing.charAt(1)=='5') column1=12; else if(choosing.charAt(1)=='6') column1=14; else if(choosing.charAt(1)=='7') column1=16; else if(choosing.charAt(1)=='8') column1=18; else if(choosing.charAt(1)=='9') column1=20; if(gameboard[row1][column1]=='\b' && ((gameboard[row1-1][column1]=='\b' && gameboard[row1-2][column1]==' ') || (gameboard[row1+1][column1]=='\b' && gameboard[row1+2][column1]==' ') || (gameboard[row1][column1-2]=='\b' && gameboard[row1][column1-4]==' ') || (gameboard[row1][column1+2]=='\b' && gameboard[row1][column1+4]==' '))) { break; } else { row1=0; column1=0; } } while((column2<4)||(column2<10&&row2<5)||(column2<10&&row2>7)||(column2>14&&row2<5)||(column2>14&&row2>7)||column>20||row<2||row>10) { System.out.print("Please choose a valid empty space you would like to move it to: "); String choosing=input.nextLine(); if (choosing.length()!=2) { choosing="A1"; } if(choosing.charAt(0)=='A' || choosing.charAt(0)=='a') row2=2; else if(choosing.charAt(0)=='B' || choosing.charAt(0)=='b') row2=3; else if(choosing.charAt(0)=='C' || choosing.charAt(0)=='c') row2=4; else if(choosing.charAt(0)=='D' || choosing.charAt(0)=='d') row2=5; else if(choosing.charAt(0)=='E' || choosing.charAt(0)=='e') row2=6; else if(choosing.charAt(0)=='F' || choosing.charAt(0)=='f') row2=7; else if(choosing.charAt(0)=='G' || choosing.charAt(0)=='g') row2=8; else if(choosing.charAt(0)=='H' || choosing.charAt(0)=='h') row2=9; else if(choosing.charAt(0)=='I' || choosing.charAt(0)=='i') row2=10; if(choosing.charAt(1)=='1') column2=4; else if(choosing.charAt(1)=='2') column2=6; else if(choosing.charAt(1)=='3') column2=8; else if(choosing.charAt(1)=='4') column2=10; else if(choosing.charAt(1)=='5') column2=12; else if(choosing.charAt(1)=='6') column2=14; else if(choosing.charAt(1)=='7') column2=16; else if(choosing.charAt(1)=='8') column2=18; else if(choosing.charAt(1)=='9') column2=20; if(gameboard[row2][column2]!=' ' || (row2!=row1-2 && row2!=row1+2 && column2!=column1-4 && column2!=column1+4)) { row2=0; column2=0; } } gameboard[row1][column1]=' '; gameboard[row2][column2]='\b'; gameboard[(row1+row2)/2][(column1+column2)/2]=' '; boolean tf=false; for (int p=1; p<11 && tf==false; p++) { for (int m=2; m<21; m++) { if(gameboard[p][m]=='\b' && ((gameboard[p-1][m]=='\b' && gameboard[p-2][m]==' ') || (gameboard[p+1][m]=='\b' && gameboard[p+2][m]==' ') || (gameboard[p][m-2]=='\b' && gameboard[p][m-4]==' ') || (gameboard[p][m+2]=='\b' && gameboard[p][m+4]==' '))) { tf=true; break; } } } if(tf==true) { System.out.println("You have " + (pegs-1) + " pegs left."); continue; } else if(tf==false) { for (int i = 1; i < 11; i++) { for (int j = 2; j < 21; j++) { System.out.print(gameboard[i][j]); } System.out.println(); } System.out.println("You have no more moves left."); play="n"; break; } } System.out.print("Would you like to play again? y/n "); play=input.nextLine(); } if(played==false) System.out.print("Thank you for playing!"); else if(played==true) System.out.println("Thank you for visiting!"); } }
|