Program do zliczania parzystych liczb.

0

Witam mam drobyny problem z programem.
Program ma sumowac liczby parzyste.

import java.util.Scanner;

class P3LoopRange
{
    public static void main(String[] args)
    {
    	Scanner input=new Scanner(System.in);

    	int firstvalue;
    	int secondvalue;
    	int sum;

    	System.out.println("Please, enter your initial value: ");
    	firstvalue=input.nextInt();

    	System.out.println("Please, enter your finish value: ");
    	secondvalue=input.nextInt();

    		if (firstvalue %2=0)
    		{
    		   for (int i=firstvalue; i<=secondvalue; i=i+2)
    		   	{
    	   		sum+=i;
    	   		System.out.println("The sum of the even numbers in the range is: " +sum);
    		   	}
    		}
    		else
    			if (firstvalue %2!=0)
    	   		{
    				for (int i=firstvalue+1; i<=secondvalue; i=i+2)
					{
		    		sum+=i;
		    		System.out.println("The sum of the even numbers in the range is: " +sum);
    		  	 	}
    	  	 	}
    }
}
0

Nie napisałeś jaki to problem, ale strzelam że wysypuje się na powiększaniu sum, bo nie jest zainicjalizowane. Daj na początku int sum = 0. Poza tym nie możesz do "firstvalue %2" podstawić zera, zmień to na ==

0

Program powinien zczytywac 2 liczby ktore uzytkownik wprowadzi i zliczyc sume pomiedzy tymi dwoma liczbami

import java.util.Scanner;

class P3LoopRange2
{
    public static void main(String[] args)
    {
    	Scanner input=new Scanner(System.in);

    	int firstvalue;
    	int secondvalue;
    	int sum=0;

    	System.out.println("Please, enter your initial value: ");
    	firstvalue=input.nextInt();

    	System.out.println("Please, enter your finish value: ");
    	secondvalue=input.nextInt();

    		if (firstvalue %2==0)
    		{
    		   for (int i=firstvalue; i<=secondvalue; i=i+2)
    		   	{
    	   		sum+=i;
    	   		//sum++;
    	   		System.out.println("The sum of the even numbers in the range is: " +sum);
    		   	}
    		}
    		else
    			if (firstvalue %2!=0)
    	   		{
    				for (int i=firstvalue+1; i<=secondvalue; i=i+2)
					{
					sum+=i;
		    		System.out.println("The sum of the even numbers in the range is: " +sum);
    		  	 	}
    	  	 	}
    }
}
0

Po co to

if (firstvalue %2!=0)

sprawdzenie?
Wynik powinieneś wypisywać dopiero po wyjściu z pętli.

0

robisz petle od liczba A do liczby B a pozniej sprawdzasz czy liczba jest parzysta, jak tak to dodajesz. napisalem glupote.

0

A moglbys mniejwiecej napisac jak by to wygladalo

0

Program powinien zczytywac 2 liczby ktore uzytkownik wprowadzi i zliczyc sume pomiedzy tymi dwoma liczbami

A może tak?

import java.util.Scanner;

public class myClass {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int p = in.nextInt();
        int q = in.nextInt();
        System.out.print((p+q)*(q-p)/4);
    }
}
 
1

A może nie? Twój wzór daje błędny wynik już dla p=q=2.

0

Teraz jest ok dziala tak jak powinien :]

import java.util.Scanner;

class P3LoopRange2
{
    public static void main(String[] args)
    {
    	Scanner input=new Scanner(System.in);

    	int firstvalue;
    	int secondvalue;
    	int sum=0;
    	

    	System.out.println("Please, enter your initial value: ");
    	firstvalue=input.nextInt();

    	System.out.println("Please, enter your finish value: ");
    	secondvalue=input.nextInt();

    		if (firstvalue %2==0)
    		{
    		   for (int i=firstvalue; i<secondvalue; i=i+2)
    		   	{
    	   		sum+=i;
    	   		System.out.println("The sum of the even numbers in the range is: " +i);
    	   	}
    		}
    		else
    			if (firstvalue %2!=0)
    	   		{
    				for (int i=firstvalue+1; i<secondvalue; i=i+2)
					{
					sum+=i%secondvalue;

					System.out.println("Range of even number is " +i );
    		 	 	}
    	  	 	}
    System.out.println("sum is :" +sum);
   }
}

2
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int p = (in.nextInt() -1) >> 1;
        int q = in.nextInt() >>1;
        System.out.print(q*q+q-p*p-p);
    }
1

Z dedykacją dla @Shalom'a`

        System.out.println("Please, enter your initial value: ");
        p=input.nextInt();
 
        System.out.println("Please, enter your finish value: ");
        q=input.nextInt();
        p+=p%2;
        q-=q%2;
        sum = (p+q)*(q-p+2)/4;
        System.out.println(sum);

Zapomniałem, że operator modulo w Javie daje dla liczb ujemnych wartość ujemną.

        p+=Math.abs(p%2);
        q-=Math.abs(q%2);
        if(p <= q){
            sum = (p+q)*(q-p+2)/4;
        }
0

Morał jest taki: kod z pętlą for jest lepszy.

0

Tak z ciekawości ... to samo zadanie tylko nieco rozszerzone w przedziale p..q zliczyć sumę liczb podzielnych przez n.
Przy n=2 - mamy oryginalne zadanie.
Ot co mi wyszło:

int fun2(int n,int p,int q)
  {
   if(p>=0) p+=n-1;
   if(q<0) q-=n-1;
   p/=n;
   q/=n;
   return n*(q-p+1)*(p+q)/2;
  }

więc pętla z cała pewnością nie jest dobrym rozwiązaniem.

1 użytkowników online, w tym zalogowanych: 0, gości: 1