Method repeating issue

I'm trying to figure out how to repeat the method while having the new one add the new input to the previous input. I haven't been able to figure this out and have been stuck on it for a while. here's what I currently have currently public static void problem1(){ System.out.println("Problem 1"); //Code for problem 1 here. int Ans; // asks the user how many temperatures they would like to input do{ Scanner input = new Scanner(System.in); System.out.println("please input the temperatures in degrees farenheit"); int temp = input.nextInt(); int[] tempNum = new int[temp]; int total = temp+temp; int average = total/tempNum[temp]; if(temp < 32){ System.out.println(" temperatures are below freezing"); } else if(temp == 32){ System.out.println(" temperatures are freezing"); } else if(temp > 32){ System.out.println(" temperatures are above freezing"); } System.out.println("The average is " + average); System.out.println("Would you like to input another temperature? Type 1 for yes and 2 for no"); Ans = input.nextInt(); //asks if the user would like to input another temp if(Ans == 2){ System.out.println("thank you and goodbye"); // shuts off when user inputs 2 System.exit(0); } else if( Ans > 2){ System.out.println("Please input a valid option"); System.exit(0); } } while(Ans == 1); // loops when user inputs 1 }
8 Replies
JavaBot
JavaBot•9mo ago
⌛ This post has been reserved for your question.
Hey @stapps! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
JavaBot
JavaBot•9mo ago
Please format your code to make it more readable. For java, it should look like this:
​`​`​`​java
public void foo() {

}
​`​`​`​
​`​`​`​java
public void foo() {

}
​`​`​`​
stapps
stappsOP•9mo ago
public static void problem1(){
System.out.println("Problem 1");
//Code for problem 1 here.
int Ans; // asks the user how many temperatures they would like to input
do{
Scanner input = new Scanner(System.in);
System.out.println("please input the temperatures in degrees farenheit");
int temp = input.nextInt();
int[] tempNum = new int[temp];
int total = temp+temp;
int average = total/tempNum[temp];
if(temp < 32){
System.out.println(" temperatures are below freezing");
}
else if(temp == 32){
System.out.println(" temperatures are freezing");
}
else if(temp > 32){
System.out.println(" temperatures are above freezing");
}
System.out.println("The average is " + average);
System.out.println("Would you like to input another temperature? Type 1 for yes and 2 for no");
Ans = input.nextInt(); //asks if the user would like to input another temp
if(Ans == 2){
System.out.println("thank you and goodbye"); // shuts off when user inputs 2
System.exit(0);
}
else if( Ans > 2){
System.out.println("Please input a valid option");
System.exit(0);
}
}
while(Ans == 1); // loops when user inputs 1
}
public static void problem1(){
System.out.println("Problem 1");
//Code for problem 1 here.
int Ans; // asks the user how many temperatures they would like to input
do{
Scanner input = new Scanner(System.in);
System.out.println("please input the temperatures in degrees farenheit");
int temp = input.nextInt();
int[] tempNum = new int[temp];
int total = temp+temp;
int average = total/tempNum[temp];
if(temp < 32){
System.out.println(" temperatures are below freezing");
}
else if(temp == 32){
System.out.println(" temperatures are freezing");
}
else if(temp > 32){
System.out.println(" temperatures are above freezing");
}
System.out.println("The average is " + average);
System.out.println("Would you like to input another temperature? Type 1 for yes and 2 for no");
Ans = input.nextInt(); //asks if the user would like to input another temp
if(Ans == 2){
System.out.println("thank you and goodbye"); // shuts off when user inputs 2
System.exit(0);
}
else if( Ans > 2){
System.out.println("Please input a valid option");
System.exit(0);
}
}
while(Ans == 1); // loops when user inputs 1
}
JavaBot
JavaBot•9mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
dan1st
dan1st•9mo ago
What exactly do you want to repeat there? Do you just want to call it again?
Unknown User
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
tjoener
tjoener•9mo ago
import java.util.Scanner;

public class Problem1 {
public static void problem1() {
System.out.println("Problem 1");
//Code for problem 1 here.
int Ans; // asks the user how many temperatures they would like to input
do {
Scanner input = new Scanner(System.in);
System.out.println("please input the temperatures in degrees farenheit");
int temp = input.nextInt();
int[] tempNum = new int[temp];
int total = temp + temp;
int average = total / tempNum[temp];
if (temp < 32) {
System.out.println(" temperatures are below freezing");
} else if (temp == 32) {
System.out.println(" temperatures are freezing");
} else if (temp > 32) {
System.out.println(" temperatures are above freezing");
}
System.out.println("The average is " + average);
System.out.println("Would you like to input another temperature? Type 1 for yes and 2 for no");
Ans = input.nextInt(); //asks if the user would like to input another temp
if (Ans == 2) {
System.out.println("thank you and goodbye"); // shuts off when user inputs 2
System.exit(0);
} else if (Ans > 2) {
System.out.println("Please input a valid option");
System.exit(0);
}
}
while (Ans == 1); // loops when user inputs 1
}

public static void main(String[] args) {
problem1();
}
}
import java.util.Scanner;

public class Problem1 {
public static void problem1() {
System.out.println("Problem 1");
//Code for problem 1 here.
int Ans; // asks the user how many temperatures they would like to input
do {
Scanner input = new Scanner(System.in);
System.out.println("please input the temperatures in degrees farenheit");
int temp = input.nextInt();
int[] tempNum = new int[temp];
int total = temp + temp;
int average = total / tempNum[temp];
if (temp < 32) {
System.out.println(" temperatures are below freezing");
} else if (temp == 32) {
System.out.println(" temperatures are freezing");
} else if (temp > 32) {
System.out.println(" temperatures are above freezing");
}
System.out.println("The average is " + average);
System.out.println("Would you like to input another temperature? Type 1 for yes and 2 for no");
Ans = input.nextInt(); //asks if the user would like to input another temp
if (Ans == 2) {
System.out.println("thank you and goodbye"); // shuts off when user inputs 2
System.exit(0);
} else if (Ans > 2) {
System.out.println("Please input a valid option");
System.exit(0);
}
}
while (Ans == 1); // loops when user inputs 1
}

public static void main(String[] args) {
problem1();
}
}
Fixed that formatting for you, added a main method and put everything in a class with imports But there's a lot of stuff wrong with this code though...
JavaBot
JavaBot•9mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server