|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Mar 2007
Città: roma
Messaggi: 115
|
[java] esercizio
import java.io.*;
class calcola { String[] num=new String[4]; public calcola(String str1,String str2,String str3,String str4) { this.num[0]=str1; this.num[1]=str2; this.num[2]=str3; this.num[3]=str4; } public void somma() { int som=0; for(int j=0;j<4;j++) { for(int k=0;k<num[j].length();k++) { som=som+num[j].charAt(k)-48; } System.out.print(som+" \n"); som=0; //azzero som altrimenti somma ogni risultato precedente con il successivo } } public static void main(String args[]) { InputStreamReader In=new InputStreamReader(System.in); BufferedReader Tastiera=new BufferedReader(In); try { String nu; String[] ar={" "," "," "," "}; for(int i=0;i<4;i++) { System.out.print("Inserisci un numero: "); nu=Tastiera.readLine(); ar[i]=nu; } calcola n=new calcola(ar[0],ar[1],ar[2],ar[3]); n.somma(); } catch(Exception E) { System.out.print("Errore"); } } } Sto facendo questo esercizio: devo sommare ogni numero di ogni cella dell'array, cioè: testo ={“11”,”357”,”552”,”453”} output della funzione: 2,15,12,12 Arrivato a questo punto, mi dovrei creare un altro metodo che mi calcoli la somma totale dei numeri contenuti nelle celle dell'array, cioè: testo={“11”,”357”,”552”,”453”} output della funzione: {“11”,”357”} --> 17 {“357”,”552”} --> 27 {“552”,”453”} --> 24 Come posso fare? |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2789
|
Usa il tag code, scritto così il codice fa male agli occhi
|
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Sep 2008
Messaggi: 2
|
Codice:
int som=0;
for(int j=0;j<4;j++)
{
for(int k=0;k<num[j].length();k++)
{
som=som+num[j].charAt(k)-48;
}
System.out.print(som+" \n");
som=0; //azzero som altrimenti somma ogni risultato precedente con il successivo
Codice:
for( int j = 0 ; j <= x && j < num.length ; j++) |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 03:44.



















