分配問:C,如何將值存儲在for循環中,是否可以在沒有數組的情況下完成?
程序必須計算房子 的表面,用戶必須輸入他的房間數量,然後程序必須要求每個房間的lenght和witdh。之後,程序必須顯示房子的表面。沒有提供公式,但房間的表面是長寬*寬。
我的應用程序的問題是,在for循環中,每個房間的所有長度和寬度的總和的值不會被記住。所以輸出是錯誤的。 它可以在沒有陣列的情況下完成嗎?因爲我還沒有上過這個課,還有很多其他任務需要完成。 非常感謝。
#include <stdio.h>
#include <stdlib.h>
main()
{
int camere,lungime,latime,scamere,i,scamera,scameratotal,scasa,total;
printf("Acest program calculeaza suprafata casei\n"); //this program lists the surface of the house
printf("Cate camere are casa?\n"); // how many rooms do you have?
scanf("%d",&camere);
for(i=0;i<camere;i++)
{
printf("Introduceti lungimea si latimea camerei %d\n",i); //enter the lenght and width of room %d
scanf("%d %d",&lungime,&latime);
scamera=lungime*latime; //surface of room %d is lenght*width
printf("Suprafata camerei %d este %d\n",i,scamera); //states the surface of room %d (1 , 2 or 3 etc.)
total = total + (lungime*latime); // total that i want the program to remember
scasa=total*camere; //surface of the house is total*number of rooms
}
printf("\nSuprafata casei este de %d metri",scasa); //the surface of the house , bad output , it's not total(sum of surfaces of each room)*number of rooms
}
是的,它可以完成沒有陣列。 – Sneftel 2014-11-06 14:58:55
請縮進您的代碼。 – 2014-11-06 15:03:08
謝謝大家,最後我的配方沒有了。 – 2014-11-06 15:44:05