Results 1 to 13 of 13
Thread: Hi and of course Help...
- 07-06-2012, 05:24 PM #1
Member
- Join Date
- Jul 2012
- Location
- New Mexico
- Posts
- 7
- Rep Power
- 0
Hi and of course Help...
Hi everybody;
Im a noob to Java, taking Information Systems classes at the local state university, and as you may have guessed I need help, I did a search and there is something similar to this in your forum from 2009 but this is still a unique question.
My assignment was to:
write a do, while loop that prompts the user to enter an interger. Then have the code square the number and display it until the number is 0.
the error Im getting says I am not declaring the variable (sum) but I have a feeling Im doing a lot more than that wrong. So it would be a great help if someone would just show me how its done, cause my course is online so I get no actual instruction.
Here is my code; be gentle.
import java.util.Scanner;
public class Squaring {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
//Keep Squaring until the input is 0
do {
System.out.print("Enter a interger to be squared(the program exits if the value is 0):");
double i = input.nextDouble();
double sum;
sum = Math.sqrt(i);
System.out.println(sum);
} while (sum > 0);
} //class
} //main
-
Re: Hi and of course Help...
So where in fact do you declare the sum variable? Usually a variable is declared by stating its type such as int or String or double, or whatever it's supposed to be, and then the variable name.
Moving from intro forum to new to java forum.
- 07-06-2012, 05:34 PM #3
Member
- Join Date
- Jul 2012
- Location
- New Mexico
- Posts
- 7
- Rep Power
- 0
Re: Hi and of course Help...
well for the sake of humoring you I thought I declared it here
double i = input.nextDouble();
double sum;
sum = Math.sqrt(i);
but now I know Im wrong let me read a bit.
- 07-06-2012, 06:06 PM #4
Member
- Join Date
- Jul 2012
- Location
- New Mexico
- Posts
- 7
- Rep Power
- 0
Re: Hi and of course Help...
I can not figure it out at the moment. I do not know how to get my code to say that I want it to square the inputted number then declare that number as a variable, also I do not know why saying double sum + Math.sqrt(i); does not declare sum as a varible meaning whatever the square root of 'i' is equal to.
it is one of those things where the answere is right in front of me but i cant see it. Help
-
Re: Hi and of course Help...
Post your latest code attempt, the one where you declare your variables (also please add [code] [/code] around your pasted code so it is readable), please post any and all error messages and any and all output. Let's reassess what you've got.
- 07-06-2012, 06:45 PM #6
Member
- Join Date
- Jul 2012
- Location
- New Mexico
- Posts
- 7
- Rep Power
- 0
Re: Hi and of course Help...
Okay error message : /tmp/jc_1954/Squaring.java:19: i is already defined in main(java.lang.String[])
int i = input.nextInt(); //define the variable 'i' as whatever is enered by user ^
1 error
My code is:
[code start] import java.util.Scanner;
public class Squaring {
public static void main (String[] args) {
int i; //declaring variable 'i' to be defined later?
double sum = Math.sqrt(i); //declaring variable sum as square root of i ?
Scanner input = new Scanner(System.in);
//Keep Squaring until the input is 0
do {
// Prompt user to enter an interger to be squared
System.out.print("Enter a interger to be squared(the program exits if the value is 0):");
int i = input.nextInt(); //define the variable 'i' as whatever is entered by user
Math.sqrt(i); //I do not know if this squares i or ho to write it so it does.
System.out.println(sum);
} while (sum != 0);
} //class
} //main [code end]
I tried to display my line of thought as clearly as possible with the comments and tried to tab clearly sorry its sloppy.
- 07-06-2012, 06:47 PM #7
Member
- Join Date
- Jul 2012
- Location
- New Mexico
- Posts
- 7
- Rep Power
- 0
Re: Hi and of course Help...
ya its hard to tell how the code is going to translate from my little rectangle up to the reply page: let me try the quote tags
Java Code:import java.util.Scanner; public class Squaring { public static void main (String[] args) { int i; //declaring variable i to be defined later? double sum = Math.sqrt(i); //declaring variable sum as square root of i ? Scanner input = new Scanner(System.in); //Keep Squaring until the input is 0 do { // Prompt user to enter an interger to be squared System.out.print("Enter a interger to be squared(the program exits if the value is 0):"); int i = input.nextInt(); //define the variable 'i' as whatever is entered by user Math.sqrt(i); //I do not know if this squares i or ho to write it so it does. System.out.println(sum); } while (sum != 0); } //class } //mainLast edited by Fubarable; 07-06-2012 at 07:00 PM. Reason: quote tags changed to code tags
-
Re: Hi and of course Help...
Um... I showed you how to use code tags in my post above.
[code] [/code] The first tag goes at the start of your code block and the second tag, the one with the slash, /, at the start goes at the bottom of the block.
Never mind, I've changed your quote tags (don't use these except for quotes please) to code tags above.
this error:
/tmp/jc_1954/Squaring.java:19: i is already defined in main(java.lang.String[])
int i = input.nextInt(); //define the variable 'i' as whatever is enered by user ^
is telling you that you are now declaring i *twice*. Just declare it once, that's it.
Also, you'll want to improve your code indenting, and I recommend that you use only 3 spaces to indent and that you use it uniformly. Your random indentations make it very hard to read your code, and this makes it hard for us to understand it and to help you.Last edited by Fubarable; 07-06-2012 at 07:05 PM.
- 07-06-2012, 07:07 PM #9
Member
- Join Date
- Jul 2012
- Location
- New Mexico
- Posts
- 7
- Rep Power
- 0
Re: Hi and of course Help...
Yes you did but as you can see Im a little slow and just came out of the new members read before you post link.. whose the goose? I am
-
Re: Hi and of course Help...
Regarding your code which I've formatted below (2-3 spaces for indents usually works best):
Please see commented lines above. Letters below correspond to the comment letters.Java Code:import java.util.Scanner; public class Squaring { public static void main(String[] args) { int i; // (A) double sum = Math.sqrt(i); // (B) Scanner input = new Scanner(System.in); // Keep Squaring until the input is 0 do { System.out.print("Enter a interger to be squared(the program exits if the value is 0):"); int i = input.nextInt(); // (C) Math.sqrt(i); // (D) System.out.println(sum); } while (sum != 0); } }
(A): It's OK to declare i and sum here if you want, but if you do, don't re-declare them as you do on line (C)
(B): No, don't try to do a calculation before the user has even entered any data.
(C): see (A) above.
(D): Math.sqrt(...) calculates the square root of a number. Check out the Math API to see what its methods do. There's a much simpler way of calculating a number's square -- multiplying the number by itself. Don't forget to assign the result to sum.
- 07-06-2012, 08:29 PM #11
Member
- Join Date
- Jul 2012
- Location
- New Mexico
- Posts
- 7
- Rep Power
- 0
Re: Hi and of course Help...
I got it to compile like this,Java Code:import java.util.Scanner; public class Squaring { public static void main (String[] args) { Scanner input = new Scanner(System.in); //prompt user to enter a number to be squared double sum; //Keep Squaring until the input is 0 do { System.out.print("Enter a interger to be squared(the program exits if the value is 0):"); int a = input.nextInt(); System.out.print(Math.sqrt(a)); sum = input.nextDouble(); } while (sum !=0); } //class } //main
I will have to wait till I get home to be able to run it. Unless you can direct me to an online compiler that can do it all, I am very restricted as to what i can download here. Thank you for your help Ill be back later I forsee having problems in the future. Lightning just took out our power and we are running on generators time to go home early i guess.
P.S. where do you get the pretty numbered programming platform with all the colors and what not?Last edited by squeek; 07-06-2012 at 08:31 PM. Reason: post script
-
Re: Hi and of course Help...
- 07-07-2012, 07:55 AM #13
Re: Hi and of course Help...
Please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks