Hi, I stuck in my program, when user gives start number for example 1 and end number 5 it should print out 1+2+3+4+5 = 15. Everything is fine it does what i want. But i want when it prints out if there is more than 10 numbers it should go to next line. for example like this 1+2+3+4+5+6+7+8+9+10 and next line for 11+12+... Please help! - import javax.swing.JOptionPane;
-
-
public class Oppgave2{
-
-
public static void main(String[] args){
-
-
int x;
-
int y;
-
String i, j;
-
-
i = JOptionPane.showInputDialog("Write start number:");
-
x = Integer.parseInt(i);
-
j = JOptionPane.showInputDialog("Write end number");
-
y = Integer.parseInt(j);
-
-
if ( x > y || x == y){
-
-
i = JOptionPane.showInputDialog("Wrong! Start number is bigger than end number!\nTry again! Write start number again:");
-
x = Integer.parseInt(i);
-
j = JOptionPane.showInputDialog("Write end number:");
-
y = Integer.parseInt(j);
-
-
}
-
-
int tall;
-
tall = x;
-
int sum = 0;
-
String sumMelding = "";
-
-
while(tall <= y){
-
-
sumMelding = sumMelding + tall + "+";
-
sum = sum + tall;
-
tall++;
-
}
-
-
sumMelding = sumMelding.substring(0, sumMelding.length() - 1);
-
-
JOptionPane.showMessageDialog(null, sumMelding + " = " + sum);
-
-
}
-
}
2 3256 Luuk 1,047
Expert 1GB
a higher number is wider than a small number, so why do you want a newline after 10 numbers?
A newline after a certain amount of characters -
1+2+3+4+5+6+7+8+9+10+
-
11+12+13+14+15+16+17+
-
18+19+20+21+22+23+45+
-
25+26+27+....
-
-
....+84+85+86+87+
-
88+89+90+91+92+93+94+
-
95+96+97+98+99+100+
-
101+102+103+104+105+
-
106+107+108+109+110
-
I found the solution ;) - import javax.swing.JOptionPane;
-
-
public class Oppgave2{
-
-
public static void main(String[] args){
-
-
int x;
-
int y;
-
String i, j;
-
int sum = 0;
-
-
i = JOptionPane.showInputDialog("Write start number:");
-
x = Integer.parseInt(i);
-
j = JOptionPane.showInputDialog("Write end number");
-
y = Integer.parseInt(j);
-
-
if ( x > y || x == y){
-
-
i = JOptionPane.showInputDialog("Wrong! Start number is bigger than end number!\nTry again! Write start number again:");
-
x = Integer.parseInt(i);
-
j = JOptionPane.showInputDialog("Write end number:");
-
y = Integer.parseInt(j);
-
-
}
-
String skriv = "";
-
for (int a = x; a < y+1; a++){
-
sum += a;
-
-
if( a == y){
-
-
skriv += a ;
-
-
}else{
-
skriv += (a) + "+";
-
}
-
if (a % 10==0){
-
-
skriv += "\n" ;
-
}
-
}
-
JOptionPane.showMessageDialog(null, skriv + "=" + sum);
-
-
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Michael R |
last post by:
Hello.
I have a rather simply question - how to display a line number in an empty control of a continous form? I can't use totals query for that form.
Thanks, Michael.
|
by: Matt F |
last post by:
I'm creating simple ping script that prompts a user for various
parameters.
However, I would like each line of the ping displayed as it
is received. Currently, the result isn't displayed until...
|
by: dheema |
last post by:
hi,
i would like to knw how to format the display of numbers from ms access database in asp page, in the database its dispalying properly but not in the asp page.
i try format currency..its not...
|
by: ashwath |
last post by:
Hi,
I am relatively new to Windows forms, i am using a listbox to show
items in Compact Framework 2.0. I want to display something of this
sort
Name: John ...
|
by: amulyab |
last post by:
Hi all,
I want to display the line numbers for the text which resides inside the text field.How can i do this?Please help me.
|
by: orked |
last post by:
i want to display number using echo, i wrote the following code
<?php
$a= 01020;
echo $a;
?>
the result :582
i don't understand the result.
this happen only if i add zero to the first place...
|
by: pod |
last post by:
let's see how fast I get an answer for this, ... I am still searching the menus in the mean time
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |