添加错误,简单的问题

时间:2017-01-26 02:19:18

标签: c++ visual-c++

菜鸟问题!当我运行这个并且我将客人数量设为2时,我应该有2个小比萨,因为数字不大于3(使其成为中等),当我运行时,它告诉我所有尺寸的0。舍入错误?我该如何解决? - 请说英文,我是初学者:)

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    //promt user
    cout << "Please enter number of guests:";
    int attendees;
    cin >> attendees;

    int large_pizzas = attendees / 7 ;
    cout << "purchase " << large_pizzas << " large pizzas" << endl;

    int medium_pizzas = large_pizzas % 3 ;
    cout << "purchase " << medium_pizzas << " medium pizzas" << endl;

    int small_pizzas = medium_pizzas % 1 ;
    cout << "purchase " << small_pizzas << " small pizzas" << endl;

2 个答案:

答案 0 :(得分:0)

没有错误。 2/7的结果不是整数,因此它已被强制转换为整数。在C ++中,转换动作只删除“。”之后的所有内容,比方说,0.32不是整数,C ++将其转换为整数,结果为0.至于“%”,结果也是正确的,你应该知道什么是“%”。

答案 1 :(得分:0)

可能你可以调整你想要的方式:

import java.util.*;
public class Storage {
public static int i = 0, size;
static int[] number;

public static void main (String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the value of array?");
    number = new int [input.nextInt()];
    System.out.println("Write the numbers:");

    for (int i : number) {
        number[i] = input.nextInt();
    }
    System.out.println("Array:");
    System.out.println(Arrays.toString(number));
}

}