Expand|Select|Wrap|Line Numbers
- 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);
- }
- }