通常的参数是(String [] arg).这个我接受一个带字符串数组的参数.当我将其更改为(Int arg)时,我得到一个运行时错误"没有这样的方法错误".我知道错误是由更改参数引起的.我的问题是我可以将参数从String数组更改为其他内容,还是只能使用预设参数?
可能重复:
为什么Java main方法是静态的?
JAVA支持反射仍然是JAVA需要主要方法为什么?
使用反射我们可以创建类的对象,甚至可以调用该类的方法,这样为什么JAVA需要main方法是静态的.
感谢您对此的评论.
我有一个Java项目(在Eclipse中运行)没有main方法,需要调试并查看哪个是调用者类和程序流.我该如何开始?
它是一个简单的项目,不包含任何web/tomcat相关数据.
谢谢你的回复.我是StackOverflow的新手,因此请原谅我的写作并提出问题.
我包括包并尝试创建类的对象,但它不识别类.所有课程都是公开的.
我已经开始学习Java,我想知道两者之间的区别:
public static void main (String [] args) {}
Run Code Online (Sandbox Code Playgroud)
和
public static void main (String args[]) {}
Run Code Online (Sandbox Code Playgroud)
如您所见,它们看起来几乎完全相同,但是我的问题是,“ []”应该在args之前还是在args之后?我有一个Java初学者指南,它向我展示了args后带有“ []”的示例。如果有人可以告诉我正确的方法,我将不胜感激。
谢谢!。
我想从函数(在示例funzione中)返回一个字符串到main.这该怎么做?谢谢!
#include <stdio.h>
#include <string.h>
#define SIZE (10)
/* TODO*/ funzione (void)
{
char stringFUNC[SIZE];
strcpy (stringFUNC, "Example");
return /* TODO*/;
}
int main()
{
char stringMAIN[SIZE];
/* TODO*/
return 0;
}
Run Code Online (Sandbox Code Playgroud)
[已编辑]对于那些需要它的人来说,以前代码的完整版本(但没有stringMAIN)是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE (10)
char *funzione (void)
{
char *stringa = malloc(SIZE);
strcpy (stringa, "Example");
return stringa;
}
int main()
{
char *ptr = funzione();
printf ("%s\n", ptr);
free (ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有主要功能:main(int argc,char*argv []).我想使用这些变量:argc,argv在其他函数中:
int function()
int main(int argc, char *argv[])
{
...
function();
...
return 0;
}
int function()
{
int b=argc;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是编译器会给出错误,即argv是未声明的.如何在其他函数中使用这些变量?
前两个参数似乎工作,但当我添加字符串时,我得到一个错误(第17行不能将字符串转换为double).我在这里错过了什么?从我在书中看到的所有内容来看,这似乎应该有效,所以我猜这是一个愚蠢的错误,但我一直在寻找最近3个小时的代码并且没有找到任何东西.感谢您阅读这篇文章!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
SimpleCalc Calc = new SimpleCalc("{0.0}", "{0.0}", "{0}");
Console.WriteLine(Calc);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是班级
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication14
{
class SimpleCalc
{
public SimpleCalc(double num1, double num2, string oper)
{
Console.Write("Enter first integer: ");
num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter operator (+,-,*, / or %)");
oper = Convert.ToString(Console.ReadLine());
Console.Write("Enter second integer: ");
num2 = …Run Code Online (Sandbox Code Playgroud) 我想问为什么我有:
int main() {
printf( "Hello world") ;
main ;
}
Run Code Online (Sandbox Code Playgroud)
编译器打印"Hello world",但是当我有main()而不是main时,它会重复打印"Hello world".
有没有办法可以访问程序的args main()而不存储对它们的引用?
程序参数存储在程序的保留空间中,因此我认为没有理由不能访问它们.也许有类似的东西const char** get_program_arguments(),int get_program_arguments_count()但我找不到它......
我的问题来自于我正在重写现在在公司内的许多程序中使用的库,我需要访问这些程序的常见参数而不更改它们.例如,我需要程序名称,但我无法使用,::getenv("_")因为它们可以从各种shell执行.我不能使用GNU扩展,因为这需要在Linux,AIX,SunOS上使用gcc,CC等工作.