Hi, im trying to write a simple program but have got majour mental block! A user has to input a number in range of 1-200 and then need to total all number in range and show the total. i.e. if 7 is entered then 1+2+3+4+5+6+7=28. As youve probably guessed I am quite new to this and am quickly falling out of love with programming as quickly as i fell in love with it.. lol Can anyone help me??? P.S. This doesnt work, yet! import java.util.Scanner; class program5 { public static void main(String[]args) { Scanner input = new Scanner(System.in); int number=0, x=0, count=1, result, num=0; System.out.println("Please input a number between 1-200."); number = input.nextInt(); for (x=1; x<=number; x++) { num=x; result = num+num; System.out.print(num++ +"+"); } } }
First read Re: How to start programming?. In short, never give up. Unlike women, if you love computers, those will love you back and that's for the lifetime. Keep my word with you. Also, believe me, your code is so nice and it is almost close. Just keep indents (tabs) so it is easy for us to read the code. Use CODE tag in the forum when you insert codes. num = 0; // initialize to zero for (x=1; x<=number; x++){ num += x; if (x != number){ System.out.print (x + " + "); } else{ System.out.print (x); // We do not print "+" at the end for the last number } } System.out.println (" = " + num); // print the final result Code (markup):
Hi NeoCambell, Thank you for your quick reply to my post. I have been meaning to reply for over a week now but only just remembering when shutting the computer down! lol I eventualy got my head round that program and have come to realise that I keep reading to much into the tutors questions on the assignment. When I asked her about it i was told that I didnt need to show the 1+2+3, only 1+3+6 ect...and the result. I am sure i had had that twice already but was using most of my brain power on trying to do the calculation by 1+2+3 etc...! OH The Stressss..! Never mind anyway, thank you again for your help. Rob
Hi bells86, I remember this one last year, a bit of a head pickeler! As I have found people within the programming comunity to be so very helpfull when I have been in the same situation, I feel I should continue the good deeds... This may help you, do try to understand it, dont just copy n paste! public static void main(String[]args) { Scanner input = new Scanner(System.in); int number=0, x=0, total=0; String answer=("y"); while (answer.equalsIgnoreCase("y")) { number=0;//sets number back to zero, so it goes back around the loop. total=0;//sets number back to zero, so it goes back around the loop. while (!(number>0 && number<=200)) { System.out.println("Please input a number between 1-200."); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); number = input.nextInt(); if (number>0 && number<=200) { for (x=1; x<=number; x++) { total=total + x; if (!(x ==number)) { System.out.print(x +" + "); } else { System.out.println(x + " = "+total); } } } else{ System.out.println("\nSorry, that number is out of range."); System.out.println("---------------------------------------"); } } System.out.println("\nWould you like to enter another number? Yes (y) or No (n)."); answer = input.next(); System.out.println("------------------------------------------------------------"); } System.out.println("Thank you, hope to see you again soon."); } Code (markup):
Thank you very much! I'm guessing you are now a seconed year lol. can't say iv found this assignment the best lol. I will read and understand not just copy and paste