小编Ord*_*rdo的帖子

在java中使用abs()方法.我的编译器不知道该方法

我有一个简单的问题,但我无法找到解决方案.

我想使用the abs ()方法,但它不起作用.我总是得到Error:" Cannot find symbol: method abs(int)".

我已经尝试通过包含"import java.math"上面的代码来使用该方法.但那也不行.

java

13
推荐指数
4
解决办法
3万
查看次数

错误:变量'string'周围的堆栈已损坏

我在下面的代码中遇到了一个小问题.这是一个简单的程序,它读入2个char和int数组.然后它将所有内容存储到另一个字符串中并将其打印出来.

#include <stdio.h>
#include <string.h>

int main ()

{
    char string [50];
    char first [11]; 
    char last [16];
    int age = 0;


    printf("Please type in your first name: ");
        scanf("%s", first); 

    printf("Please type in your last name: ");
        scanf("%s", last); 

    printf("Please type in your age: ");
        scanf("%d", &age); 

    sprintf(string, "Your name is %s %s and you are %d years old.", first, last, age);
        puts(string);

    getchar();
    getchar();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在程序运行正常,但是当我关闭它时,我收到以下错误: 运行时检查失败#2 - 变量'string'周围的堆栈已损坏. 这有点令人困惑,我无法弄清楚问题出在哪里.我会感谢任何建议.

c

6
推荐指数
1
解决办法
3万
查看次数

使用malloc()和sizeof()在堆上创建结构

我正在尝试使用malloc()和sizeof()在堆上创建一个结构.这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Employee
{
    char first[21];
    char last[21];
    char title[21];
    int salary;
};


struct Employee* createEmployee(char* first, char* last, char* title, int salary) // Creates a struct Employee object on the heap.
{
    struct Employee* p = malloc(sizeof(struct Employee)); 

    if (p != NULL)
    {
        strcpy(p->first, first);
        strcpy(p->last, last);
        strcpy(p->title, title);
        p->salary, salary;
    }
    return p;

}
Run Code Online (Sandbox Code Playgroud)

没有我的编译器(Visual C++)告诉我该行struct Employee* p = malloc(sizeof(struct Employee));"void*"无法转换为"Employee*"类型.我不知道这里有什么问题.似乎struct Employee是一个空白,但我不明白为什么......

c++

4
推荐指数
2
解决办法
2万
查看次数

函数和数组

我的下面的小程序将从用户获取5个数字,将它们存储到整数数组中并使用函数将它们打印出来.真诚地它不起作用并且没有打印出来.我找不到错误,所以我很乐意提出任何建议.谢谢.

#include <stdio.h>


void printarray(int intarray[], int n)

{
    int i;

    for(i = 0; i < n; i ++)
    {
        printf("%d", intarray[i]);
    }
}

int main ()

{
    const int n = 5;
    int temp = 0;
    int i;
    int intarray [n];
    char check;

    printf("Please type in your numbers!\n");

    for(i = 0; i < n; i ++)
    {
        printf("");
            scanf("%d", &temp);         
        intarray[i] = temp;

    }

    printf("Do you want to print them out? (yes/no): ");
        scanf("%c", &check);

        if (check == 'y') …
Run Code Online (Sandbox Code Playgroud)

c

4
推荐指数
2
解决办法
267
查看次数

C中数组中的终止NULL

我有一个简单的问题.
为什么有必要考虑字符数组(或简称字符串)中的终止空值而不是整数数组.所以,当我想要一个字符串来容纳20个字符时,我需要声明char string[21];.当我想声明一个包含5位数的整数数组时int digits[5];就足够了.这是什么原因?

c string

3
推荐指数
1
解决办法
4214
查看次数

在Java中与字符串相比较

我正在寻找一种比较两个字符串的解决方案.我已经找到了一些建议,但我并不真正理解如何做到这一点.我想做那样的事情:

String a = 23;
String b = 2;

if (a < b)
System.out.println("...");
else ..
Run Code Online (Sandbox Code Playgroud)

我找到了compareTo方法,但我不明白它背后的想法.代码如下:

String a = 23;
String b = 2;

if (a.compareTo(b) < 0)
System.out.println("...");
else ..
Run Code Online (Sandbox Code Playgroud)

但为什么我要相互比较字符串,然后将它与零比较?我真的很困惑.

java

2
推荐指数
2
解决办法
640
查看次数

使用函数和数组

我的下面的小程序将从用户获取5个数字,将它们存储到整数数组中并使用函数将它们打印出来.真诚地它不起作用,我的输出总是"00000".我找不到错误,所以我很乐意提出任何建议.谢谢.

#include <stdio.h>

void printarray(int intarray[], int n)
{
    int i;
    for(i = 0; i < n; i ++)
    {
        printf("%d", intarray[i]);
    }
}

int main ()    
{
    const int n = 5;
    int temp = 0;
    int i;
    int intarray [n];
    char check;

    printf("Please type in your numbers!\n");

    for(i = 0; i < n; i ++)
    {
        printf("");
            scanf("&d", &temp);         
        intarray[i] = temp;


        getchar();
        getchar();

    }

    printf("Do you want to print them out? (yes/no): ");
        scanf("%c", &check);

        if (check …
Run Code Online (Sandbox Code Playgroud)

c

2
推荐指数
1
解决办法
505
查看次数

查找整数数组中的最小数字

我编写了一个小程序,它从用户那里获取5个数字并将它们存储在一个整数数组中.该数组被传递给一个函数.该函数用于查找数组中的最小数字并将其打印出来.由于输出不正确,我不知道为什么.该函数总是打印出数组的第一个元素,它应该是最小的数字,但事实并非如此.

#include <stdio.h>

void smallestint (int intarray [], int n)

{
    int i;
    int temp1 = 0;
    int temp2 = 0;

    for(i = 0; i < n; i ++)
    {

        if (intarray [i] < temp1)
        {
            intarray [i-1] = intarray [i];
            temp2 = intarray[i];
            intarray[i] = temp1;
            temp1 = temp2;
        }
        else 
            temp1 = intarray[i];
    }

    printf("%d\n", intarray[0]);
}

int main ()

{
    const int n = 5;
    int temp = 0;
    int i;
    int intarray [n];

    printf("Please type in …
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
2万
查看次数

在VIsual C++中使用C++代码,只会忽略错误,但代码的某些部分会被忽略

我是编程的绝对初学者,我只是在开始时做一些练习练习.

首先,我使用Visual C++ 2010编译C代码.我只是创建一个新项目并选择一个空的控制台应用程序.之后,我创建一个名为test.c的资源文件,并将文件属性中的元素类型更改为C/C++编译器并编译为C++代码,以便我可以使用#include <iostream>std::cin.get()命令.现在的代码:

#include <stdio.h>
#include <iostream>

int main() 
{
   int number1, number2;
   int sum;

   puts("Enter number 1 please:");
      scanf_s("%d",&number1);
   puts("Enter number 2 please:");
      scanf_s("%d",&number2);

   std::cin.get();
   std::cin.get();  //(1)

   sum = number1 + number2;
      printf("The average is %f\n", sum/2);

return 0; 
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,只是忽略了"std :: cin.get()"命令.输入两个数字后,程序就会停止,控制台窗口关闭.

知道问题出在哪里?

我还有另一个问题.

由于我解决了控制台打开的问题(1),现在我的printf()只给出零作为输出.我希望有一个浮点数作为输出,但无论我输入的是number1和number2,我总是得到"0.000000".

由于我还在处理我的小程序以在接受之前验证输入,我还有另一个问题.

我想使用以下代码来检查输入.

#include <stdio.h>
#include <iostream>
#include <ctype.h>

int main() 
{
   int number1, number2;
   int sum;

   puts("Enter number 1 please:");
      scanf_s("%d",&number1);

   if (isdigit(number1)) 
   {
   puts("Enter number 2 …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++

0
推荐指数
1
解决办法
401
查看次数

使用float计算C/C++

我正在练习C,编写简单的程序.下面的小程序应该从用户那里获得3个数字并将它们相乘.我的问题是我对我必须使用的变量类型有点困惑.我希望程序采用任何数字,如5,5.673434,99.123等,用它们计算并打印出一个圆形浮点数.我尝试了很多东西但是结果总是错误的.该程序只打印出非常大的数字和令人困惑的字符序列.我会感谢任何建议.谢谢.

#include <stdio.h>

int main()
{
    int num1, num2, num3;

    printf("Hello! This little programm will execute a few calculations \nafter you've typed in 3 numbers of your choise. \nPlease type in your first number: ");
    scanf_s("%f", &num1);
    printf("Great. Please choose your second number: ");
    scanf_s("%f", &num2);
    printf("And the third number please: ");
    scanf_s("%f", &num3);
    printf("Ok. You want to use %f, %f, %f for your calculation. Press a button begin.\n", num1, num2, num3 );  
    printf("Multiplication: %.2f", num1 * num2 …
Run Code Online (Sandbox Code Playgroud)

c c++

0
推荐指数
1
解决办法
1292
查看次数

用Java递归整数整数

我必须使用递归算法计算两个整数的总和,但真诚地我不知道如何这样做.以下是条件:

sum(x,y)=?
如果 x = 0 sum(x,y)= y 否则 sum(x,y)= sum(前趋(x),后继(y)).

有人知道如何在算法中写这个吗?任何建议我都会很高兴.

java algorithm

0
推荐指数
1
解决办法
1398
查看次数

标签 统计

c ×6

c++ ×3

java ×3

algorithm ×1

string ×1

visual-c++ ×1