Showing posts with label For Loop. Show all posts
Showing posts with label For Loop. Show all posts

All in One: SOLVED!

So, now we finished the program we are making - A game. So here's the code, check it out:

import java.io.*;
public class QnA
{
public static void main (String args[]) throws IOException
{
DataInputStream old = new DataInputStream (System.in);

int startexit;

int[][] love = {{3,4,5},{6,7,8,9}};
int[][] heart= {{1,2},{6,7,8,9}};
int[][] ambot = {{1,2},{3,4,5},};

String name;

System.out.println("Enter your name:");
name = old.readLine();


System.out.println("\t \t =============================================== \t \t");
System.out.println("\t \t =============================================== \t \t");
System.out.println("\t \t +++++++++++++ Questions & Answers +++++++++++++ \t \t");
System.out.println("\t \t =============================================== \t \t");
System.out.println("\t \t =============================================== \t \t");

System.out.println("\n\n\n Greetings " + name + "!");

System.out.println ("\n First Round: \n Instruction: In order to proceed to the the second and final round, you have to get 3 correct answers. Each correct answer costs 1 point only. Press only the number of your choice. \n Note: Invalid Choices are considered Wrong.");

System.out.println("\nPress 1 to Start or press any number to exit");
startexit = Integer.parseInt(old.readLine());

if (startexit == 1)
{

System.out.println("\t \t =============================================== \t \t");
System.out.println("\t \t +++++++++++++ Questions & Answers +++++++++++++ \t \t");
System.out.println("\t \t +++++++++++++++++ First Round +++++++++++++++++ \t \t");
System.out.println("\t \t =============================================== \t \t");

int n=0;
int correct=0;

// System.out.println ("\nStage 2");
System.out.println ("\nQuestion # 1");
System.out.println ("\nWhat is the Oldest Street in the Philippines?");
System.out.println ("\n1. Orion St.,C.D.O. City");
System.out.println ("\n2. Sulu St.,Sta Cruz Manila");
System.out.println ("\n3. Colon St.,Cebu");
System.out.println ("\n4. Quezon ave.Manila\n");

n=Integer.parseInt (old.readLine());

switch (n)
{
case 1: System.out.println ("\nWrong");
break;
case 2: System.out.println ("\nWrong");
break;
case 3: System.out.println ("\nCorrect");
correct = correct +1;
break;
case 4: System.out.println ("\nWrong");
break;
default : System.out.println ("\nInvalid Choice");
}


System.out.println ("\nQuestion # 2");
System.out.println ("\nWhat is the most consumed fruit in the world?");
System.out.println ("\n1. Grapes");
System.out.println ("\n2. Bananas");
System.out.println ("\n3. Apple");
System.out.println ("\n4. Mangoes\n");

n=Integer.parseInt (old.readLine());

switch (n)
{
case 1: System.out.println ("\nWrong");
break;
case 2: System.out.println ("\nCorrect");
correct = correct +1;
break;
case 3: System.out.println ("\nWrong");
break;
case 4: System.out.println ("\nWrong");
break;
default : System.out.println ("\nInvalid Choice");
}

System.out.println ("\nQuestion # 3");
System.out.println ("\nWhat is Biggest animal in the world?");
System.out.println ("\n1. blue whales");
System.out.println ("\n2. elephants");
System.out.println ("\n3. Polar Bears");
System.out.println ("\n4. flies and mosquitoes");

n=Integer.parseInt (old.readLine());

switch (n)
{
case 1: System.out.println ("\nCorrect");
correct = correct +1;
break;
case 2: System.out.println ("\nWrong");
break;
case 3: System.out.println ("\nWrong");
break;
case 4: System.out.println ("\nWrong");
break;
default : System.out.println ("\nInvalid Choice");
}

System.out.println ("\nQuestion # 4");
System.out.println ("\nWho is the Greek Goddess of Wisdom?");
System.out.println ("\n1. Apollo");
System.out.println ("\n2. Demeter");
System.out.println ("\n3. Athena");
System.out.println ("\n4. Zeus\n");

n=Integer.parseInt (old.readLine());

switch (n)
{
case 1: System.out.println ("\nWrong");
break;
case 2: System.out.println ("\nWrong");
break;
case 3: System.out.println ("\nCorrect");
correct = correct +1;
break;
case 4: System.out.println ("\nWrong");
break;
default : System.out.println ("\nInvalid Choice");
}

System.out.println ("\nQuestion # 5");
System.out.println ("\nWhat is the largest city in the world?");
System.out.println ("\n1. Tokyo-Yokohama, Japan");
System.out.println ("\n2. Manila, Philippines");
System.out.println ("\n3. New York, United States");
System.out.println ("\n4. Sao Paulo, Brazil\n");

n=Integer.parseInt (old.readLine());

switch (n)
{
case 1: System.out.println ("\nCorrect");
correct = correct +1;
break;
case 2: System.out.println ("\nWrong");
break;
case 3: System.out.println ("\nWrong");
break;
case 4: System.out.println ("\nWrong");
break;
default : System.out.println ("\nInvalid Choice");
}

for (int ctr = 50; ctr>0; ctr--)
{
System.out.println("Loading");
}

System.out.println ("\n\nCorrect answers:" + correct);

if (correct < 3) { System.out.println("Sorry, you failed. You cannot proceed to the final level"); System.out.println("\n\nGoodbye!" + " " + heart[0][0] + love[0][1] + ambot[1][0] + "! :)"); } else { System.out.println("\n\nCongratulations! You passed and you can now proceed to the next level"); int decision; System.out.println("\nDo you wish to continue? Press 1 if yes or press any number if no."); decision = Integer.parseInt(old.readLine()); if (decision == 1) { System.out.println("\t \t =============================================== \t \t"); System.out.println("\t \t +++++++++++++ Questions & Answers +++++++++++++ \t \t"); System.out.println("\t \t +++++++++++++++++ Final Round +++++++++++++++++ \t \t"); System.out.println("\t \t =============================================== \t \t"); System.out.println ("\n Final Round: \n Instruction: In order to win the game, you have to get at least 9 points. Each correct answer costs 2 points only. Type the number of your answer only \n Note: Points in the first round will be added. Invalid Choices are considered Wrong."); int answers; System.out.println("\n\nWhat festival is celebrated in Cebu every third Sunday of January? "); System.out.println("\n1.Sinulog \n2.Durian\n3.Albay\n4.Nile River\n5.San Lorenzo Ruiz"); answers = Integer.parseInt(old.readLine()); switch (answers) { case 1 : System.out.println("Correct!"); correct = correct + 2; break; case 2 : System.out.println("Wrong!"); break; case 3 : System.out.println("Wrong!"); break; case 4 : System.out.println("Wrong!"); break; case 5 : System.out.println("Wrong!"); break; default: System.out.println("Wrong!"); break; } System.out.println("\nWho was the first Filipino Saint? "); System.out.println("\n1.Sinulog \n2.Durian\n3.Albay\n4.Nile River\n5.San Lorenzo Ruiz"); answers = Integer.parseInt(old.readLine()); switch (answers) { case 1 : System.out.println("Wrong!"); break; case 2 : System.out.println("Wrong!"); break; case 3 : System.out.println("Wrong!"); break; case 4 : System.out.println("Wrong!"); break; case 5 : System.out.println("Correct!"); correct = correct + 2; break; default: System.out.println("Wrong!"); break; } System.out.println("\nWhat is the longest river in the world? "); System.out.println("\n1.Sinulog \n2.Durian\n3.Albay\n4.Nile River\n5.San Lorenzo Ruiz"); answers = Integer.parseInt(old.readLine()); switch (answers) { case 1 : System.out.println("Wrong!"); break; case 2 : System.out.println("Wrong!"); break; case 3 : System.out.println("Wrong!"); break; case 4 : System.out.println("Wrong!"); correct = correct + 2; break; case 5 : System.out.println("Wrong!"); break; default: System.out.println("Wrong!"); break; } System.out.println("\nWhere can you find Mayon Volcano?"); System.out.println("\n1.Sinulog \n2.Durian\n3.Albay\n4.Nile River\n5.San Lorenzo Ruiz"); answers = Integer.parseInt(old.readLine()); switch (answers) { case 1 : System.out.println("Wrong!"); break; case 2 : System.out.println("Wrong!"); break; case 3 : System.out.println("Wrong!"); correct = correct + 2; break; case 4 : System.out.println("Wrong!"); break; case 5 : System.out.println("Wrong!"); break; default: System.out.println("Wrong!"); break; } for (int ctr = 50; ctr>0; ctr--)
{
System.out.println("Loading");
}

System.out.println("\n\n\n");

if (correct >=9)
{
System.out.println("Congratulations! You won!");
System.out.println ("\nOverall Score:" + correct);


System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~ Congratulations! ~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
}
else
{
System.out.println("Sorry you failed. Better luck next time");
System.out.println ("\nOverall Score:" + correct);
}
}

else
System.out.println("\n\nGoodbye!" + " " + heart[0][0] + love[0][1] + ambot[1][0] + "! :)");

}


}
else

System.out.println("\n\nGoodbye!" + " " + heart[0][0] + love[0][1] + ambot[1][0] + "! :)");
}
}


Programmers:

All in One

I'm in the middle of creating a program [Java] with the help of two other pals I got in school. We will actually defend that program within this week. We are required to use four out of the five statements, namely:

  • If statement (which includes the Dangling If's and others)
  • Arrays (Whether the normal array or the 2D)
  • Switch (commonly known as Case)
  • Loop (any of the loops)
So array is divided into two which means we have the power to choose which array to use, and that makes it four.

I'll post the code as soon as we are able to merge our codes. Further discussions would be available regarding the program.

Problem 2: Arrays

Based from the previous problem about arrays, here's another thing which is similar to that problem: Create a program that will allow the user to enter ten numbers and sort the numbers in an ascending and descending order. Then, display the highest and the lowest value of the ten numbers given by the user.

import java.io.*;
import java.util.Arrays;

public class Oc13
{
public static void main (String args[]) throws IOException
{

DataInputStream whatever = new DataInputStream (System.in);

int earth[] = new int [10];
int temp;

System.out.println("Enter 10 numbers: ");

for (int i = 0; i< earth.length/2; ++i ) { temp = earth[i]; earth[i] = earth[earth.length - i - 1]; earth[earth.length - i - 1] = temp; } System.out.println("Descending Order: " + Arrays.toString(earth)); System.out.println("Highest is " + earth[0]); // System.out.println("Lowest is " + earth[10]); } }

Problem: Loop

create a program based on the sample output below:

Enter Number: 5
*
**
***
****
*****


import java.io.*;
class s31
{
public static void main (String args[]) throws IOException
{
DataInputStream ethel=new DataInputStream (System.in);

int ctr;

System.out.println("Enter number ");
ctr=Integer.parseInt(ethel.readLine());


for(int i = 1; i <=ctr; i++)

{ for(int j = 1; j <=i; j++)
{ System.out.print("*"); }
System.out.println(); } } }

Loops

There are three different kinds of loops: while, for, and do-while loops.

The uses of these loops are almost alike. But, there are some cases that they're different. It only depends on how you use it.

FOR LOOP

  • repeats for a specified number of times
Syntax:
for(initialization; comparison ;increment or decrement)
{statements;}

WHILE LOOP
  • a loop that repeats until a certain condition is satisfied
Syntax:
while(condition)
{statements;)


DO-WHILE LOOP
  • unlike the while loop, the do-while loop tests the condition at the end of the loop
Syntax:
do
{statements;}
while(condition);

Extended Search

Custom Search
Hello World