Help me please

Jabu

Well-Known Member
MisterColin
MisterColin
Dec 22, 2016
430
1,496
93
20
Canada
So I'm in a computer science class, and the teacher teaches us nothing and I have no clue what I am suppose to be doing. This coding stuff is in eclipse. All the teacher gives us is a google docs that has little info. This is one of the docs.

The while loop


Another form of loop is called the while loop. This loop can have two different forms:


while (condition) {

//do stuff

// this code will be executed as long as condition is true

// if condition is initially false, this code will never be executed

}


The other form is called a do while loop:


do {

// do stuff

// this code will be executed at least once

} while (condition)


The difference between the loops is that in the second form (do...while), the code is executed at least once.



Try these out:

Make a new package and name it dowhileloops for the following code.


boolean loopflag = false;

while (loopflag) {

System.out.println("hey, do you even print?");

}


do {

System.out.println("how many times does this print?");

} while (loopflag);


int i = 0;

while (i < 10) {

System.out.println("i is " + i);

++i;

}




int j=1;

while ( j > 0) {

System.out.println("How do you stop this? " + j);

j=j+1;

}


Exercises:


Put your answers to the following questions in the dowhileloops package.

  1. Create a class called DoLoop. In the main method, write a do loop that will print out the numbers from 0 to 10.
  2. Create a class called DoWhileLoop1. In the main method, write a do while loop that will take an integer from the keyboard and loop until the user guesses the correct number.
  3. Create a class called DoWhileLoop2. In the main method, write a do while loop that will take a string from the keyboard and loop until the user guesses the correct password.
  4. Create a class called DoWhileLoop3. In the main method, write a while loop that will print out the characters from A to Z.
So please help meeeeeeee. I've already done #1 so I just need help with 2-4.
 

TheDiamondTiger

Cyber Bullied
Maggot
Feb 19, 2017
1,294
1,531
113
Diamond Cave, Quartz Forest, Kepler 186f
So I'm in a computer science class, and the teacher teaches us nothing and I have no clue what I am suppose to be doing. This coding stuff is in eclipse. All the teacher gives us is a google docs that has little info. This is one of the docs.

The while loop


Another form of loop is called the while loop. This loop can have two different forms:


while (condition) {

//do stuff

// this code will be executed as long as condition is true

// if condition is initially false, this code will never be executed

}


The other form is called a do while loop:


do {

// do stuff

// this code will be executed at least once

} while (condition)


The difference between the loops is that in the second form (do...while), the code is executed at least once.



Try these out:

Make a new package and name it dowhileloops for the following code.


boolean loopflag = false;

while (loopflag) {

System.out.println("hey, do you even print?");

}


do {

System.out.println("how many times does this print?");

} while (loopflag);


int i = 0;

while (i < 10) {

System.out.println("i is " + i);

++i;

}




int j=1;

while ( j > 0) {

System.out.println("How do you stop this? " + j);

j=j+1;

}


Exercises:


Put your answers to the following questions in the dowhileloops package.

  1. Create a class called DoLoop. In the main method, write a do loop that will print out the numbers from 0 to 10.
  2. Create a class called DoWhileLoop1. In the main method, write a do while loop that will take an integer from the keyboard and loop until the user guesses the correct number.
  3. Create a class called DoWhileLoop2. In the main method, write a do while loop that will take a string from the keyboard and loop until the user guesses the correct password.
  4. Create a class called DoWhileLoop3. In the main method, write a while loop that will print out the characters from A to Z.
So please help meeeeeeee. I've already done #1 so I just need help with 2-4.
Why are you learning java? Python has a distro that uses the jvm and C# is a thousand times more versatile with cross compiling. Java does have its uses, but it has become increasingly irrelevant now that universal language support has reached most common langs.

2)

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class DoWhileLoop1 {

public static void main(String []args) {
int r = ThreadLocalRandom.current().nextInt(0, 100 + 1);
int n;
Scanner input = new Scanner(System.in);

do {

System.out.println("Input an integer");

} while ((n = input.nextInt()) != r);
System.out.println("Correct");
}
}
3)

import java.util.Scanner;

public class DoWhileLoop2 {

public static void main(String []args) {
String s = "string";
String n;
Scanner input = new Scanner(System.in);

do {

System.out.println("Input a string");

} while (!(n = input.next()).equals(s));
System.out.println("Correct");
}
}

4)
public class DoWhileLoop3 {

public static void main(String []args) {
char c = 'a';
while (c <= 'z') {
System.out.println(c);
c++;
}
}
}
 

Jabu

Well-Known Member
MisterColin
MisterColin
Dec 22, 2016
430
1,496
93
20
Canada
Why are you learning java? Python has a distro that uses the jvm and C# is a thousand times more versatile with cross compiling. Java does have its uses, but it has become increasingly irrelevant now that universal language support has reached most common langs.

2)

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class DoWhileLoop1 {

public static void main(String []args) {
int r = ThreadLocalRandom.current().nextInt(0, 100 + 1);
int n;
Scanner input = new Scanner(System.in);

do {

System.out.println("Input an integer");

} while ((n = input.nextInt()) != r);
System.out.println("Correct");
}
}
3)

import java.util.Scanner;

public class DoWhileLoop2 {

public static void main(String []args) {
String s = "string";
String n;
Scanner input = new Scanner(System.in);

do {

System.out.println("Input a string");

} while (!(n = input.next()).equals(s));
System.out.println("Correct");
}
}

4)
public class DoWhileLoop3 {

public static void main(String []args) {
char c = 'a';
while (c <= 'z') {
System.out.println(c);
c++;
}
}
}
idk. and thanks. I may need more help later
 
  • Like
Reactions: lostinthewqrld

Powerfull

Well-Known Member
Former Staff
Verified
Powerfull
Powerfull
Citizen
Mar 22, 2017
2,012
4,754
113
well idont speak martian, I tried to learn once, but kiri and coban's combined efforts couldnt get thru my brain xD

All jokes aside, I did try and learn coding (i regret bugging kiri and coban butttt :shrug: and idek how java works still, good luck tho jabu
 

TheDiamondTiger

Cyber Bullied
Maggot
Feb 19, 2017
1,294
1,531
113
Diamond Cave, Quartz Forest, Kepler 186f
well idont speak martian, I tried to learn once, but kiri and coban's combined efforts couldnt get thru my brain xD

All jokes aside, I did try and learn coding (i regret bugging kiri and coban butttt :shrug: and idek how java works still, good luck tho jabu
Java is one of the more annoying languages, it does deserve a lot of credit for inclusivity and still has some really important uses. Although the annoying anti-assumption approach bothers me because I code for speed, efficiency and data sciences; which means my code should be atleast fairly readable with comments (although I still comment like a good boy). Currently I'm working on a project sniffing udp packets and interpreting them to find player locations in a few video games.
 
  • Like
Reactions: Powerfull