这里的问题是,在函数中已经有一些语句后,我无法在函数内声明变量.在开始时声明工作正常,但在某些事情之后,它会给出一个解析错误.例如:
int main()
{
int b;
b = sisesta();
float st[b];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想声明一个数组st,其大小由另一个函数返回,但它不会让我这样做!说"浮动前解析错误".顺便说一句,这是在C中,但我猜它与其他语言中的语法相同.
任何帮助赞赏.
有没有办法使变量/方法/类声明的默认访问修饰符公开?
我认为默认情况下,类声明是私有的吗?
我想尝试编写一个函数来自动化检查/声明变量的一些工作,即
function checkVariable($var)
{
if(!isset($var)||empty($var))
{
return '';
}
else
{
return $var;
}
}
$myvar = checkVariable($myvar);
Run Code Online (Sandbox Code Playgroud)
显然,这不会起作用,因为变量在声明之前不存在,并且当你将它用作参数时抛出错误 - sooooo,有没有办法做到这一点?
请看下面的内容:
a = 5
print a + b
b = 4
Run Code Online (Sandbox Code Playgroud)
当我尝试运行上面的代码时,它会给出一个错误:Traceback(最近一次调用最后一次):文件"C:/Users/user/Documents/modules/ab.py",第2行,打印a + b NameError:名称'b'未定义
好.被定义a + b之前调用b.这意味着Python按顺序运行代码,从上到下开始.但是,这个怎么样:
class Data:
def __init__(self):
self.debug_level = 9
self.assign = [0, 0, 0, 0]
self.days = 0
def create_days(self, startTime, endTime):
res = 0
try:
if self.final_days < self.maximum_days:
Run Code Online (Sandbox Code Playgroud)
以上,self.final_days并且self.maximum_days尚未定义要么,但它不会给任何错误.它背后的逻辑是什么?
最好的祝福,
我目前正在学习计算机科学,并希望了解所有内容,所以我在书中注意到人们声明了一个变量int x;然后在声明的正下方初始化它,例如
int x;
x = 0;
Run Code Online (Sandbox Code Playgroud)
我想知道这是否对编译代码的内存或效率有任何积极意义?
我正在尝试在C中的main方法中创建一个循环,它不是在编译.
这是代码:
int i = 1;
while (i < 10)
{
i++;
}
int x;
int a;
int b;
int sum;
sum = a + b;
Run Code Online (Sandbox Code Playgroud)
它没有编译,因为这个错误:
Error 3 error C2143: syntax error : missing ';' before 'type'
Run Code Online (Sandbox Code Playgroud)
循环后声明的所有变量中都会出现此错误.
但是,如果我在这些变量声明之后编写循环,它就可以正常工作.
那么为什么我不能在变量声明之前写一个循环呢?
在我离开过去的RealBasic(现在的Xojo)中,我曾经能够声明一个这样的方法:
Sub MyCoolSub(param1 as string, Assigns parameter2 as integer)
Do
'Waste CPU time scrying the universe.
Loop
End Sub
Run Code Online (Sandbox Code Playgroud)
然后以这种方式调用它:
MyCoolSub("Answer")=42
Run Code Online (Sandbox Code Playgroud)
现在我想在VB.Net中复制这种行为.
我偶然发现的最接近的是Property的子句,但是VS不允许我为它添加参数,但是需要一些开销会降低这种声明的便利性.
你有更好的建议吗?
PS.作为一个附带问题,我很高兴知道有一种方法可以在VB.Net中用"//"进行评论,因为我对撇号字符不太满意.作为VS评论字符列表有什么东西吗?也许扩展可以做到......
我正在尝试将下一个代码从Objetive-C迁移到Swift:
NSArray *voices = [AVSpeechSynthesisVoice speechVoices];
NSArray *languages = [voices valueForKey:@"language"];
NSLocale *currentLocale = [NSLocale autoupdatingCurrentLocale];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSString *code in languages)
{
dictionary[code] = [currentLocale displayNameForKey:NSLocaleIdentifier value:code];
}
Run Code Online (Sandbox Code Playgroud)
我做了以下事情:
var voices:NSArray = AVSpeechSynthesisVoice.speechVoices()
var languages:NSArray=voices.valueForKey("language") as NSArray
var currentLocale:NSLocale=NSLocale.autoupdatingCurrentLocale()
var dictionary:NSMutableDictionary=NSMutableDictionary()
for code in languages {
var name=currentLocale.displayNameForKey(NSLocaleIdentifier, value: code)
dictionary[code]=name
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误:类型'AnyObject'不符合协议'NSCopying'字典[code] = name
我不知道如何声明字典对象,做一些像国家代码字符串作为键和小描述的数组一样简单的事情.喜欢
字典["es-ES"] = ["西班牙语"]字典["en-US"] = ["美式英语"]
我得到一个错误"这里不允许变量声明",我不知道为什么,我是java的新手并且找不到答案:/正如它所说,我不能在"if"中创建"int"但是有没有办法创造它?
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;import java.util.Scanner;
public class test{
public static void main(String[] args) throws FileNotFoundException{
File plik = new File("test.txt");
PrintWriter saver = new PrintWriter("test.txt");
int score = 0;
System.out.println("Q: What's bigger");
System.out.println("A: Dog B: Ant");
Scanner odp = new Scanner(System.in);
string odpo = odp.nextLine();
if(odpo.equals("a"))
int score = 1;
else
System.out.println("Wrong answer");
}
}
Run Code Online (Sandbox Code Playgroud) declaration ×10
variables ×4
c ×3
java ×2
arrays ×1
c# ×1
c++ ×1
closures ×1
compilation ×1
css ×1
dictionary ×1
if-statement ×1
int ×1
loops ×1
objective-c ×1
php ×1
python ×1
realbasic ×1
scope ×1
swift ×1
vb.net ×1
while-loop ×1
xojo ×1